Is it possible to retrieve a div ID from another file via a javascript from one file.
Example:
Lets say you are editing index.html and want to retrieve an ID, using getElemntById fr开发者_如何转开发om page2.html.
I understand that you can use document.getelementbyid; if you want to get the id from the same document, but is there anything you can use to get it from another document?
Thanks in advance
You will have to make an AJAX call to page2 first.
Paste the response of XMLHTTPObject in some invisible div.
Then get the required div that you want to retrieve.
Personally, I would create an include file with the content you require on both pages, and include it on both pages. Then it would just be a matter of showing/hiding the div.
You can do this fairly easily with AJAX - you have to make an asynchronous call to page2, and then process the response to get the element you're looking for. Look up jQuery's get() method for an easy AJAX implementation - it handles the details of the XMLHTTPRequest for you.
It's also worth noting that if page2 is a page served by your server, an even easier solution is just to copy whatever information you need from page2 to page1 on the server side. Alternatively, if you do need to do an AJAX call because it isn't feasible to put all the information you might need onto page1 from the beginning, you can have your server (using php, Rails, Django, etc.) return different information for an AJAX call. So you can have it return only the info you need, rather than returning the entirety of page2.
精彩评论