I have a page called results.php that has a hidden iframe and a DIV container. When the page is loaded, some code is run in the hidden iframe which, when complete, loads the results into the CONTENT DIV in the results page, generating a scrollbar to results.php
All this works fine, but when I have anchors in the code generated within the DIV (and finally displayed by results.php) they do not work. I have tried location.hash, scrollto and others, and none work. If I display page source code, I do not see any of the contents (as they were created in the iframe and then loaded into the RESULTS DIV), which is probably why location.hash and such don't work. Also, getElementById does not see any of the anchors, I gu开发者_StackOverflow中文版ess because of the same reason.
Any ideas how to get this to work?
Thanks!
The page loaded into the IFRAME has a different context than the page containing the DIV. If you are trying to access the outer contents from the inner IFRAME, you have to go through contortions to get the parent document from the nested document. That may vary depending upon browser.
Assuming your iframe has id 'results', and your anchor has id (not name) 'my_anchor', this should work:
var iframe_doc = document.getElementById('results').contentWindow.document;
var anchor = iframe_doc.getElementById('my_anchor');
anchor.scrollIntoView();
精彩评论