问题
I have an iframe on my page, and need to get a child element from it whose ID I have. I'd like to do something like this:
document.getElementById('iframeID').getElementById('child element ID')
I'm open to suggestions on how to accomplish this. Unfortunately I don't have access to jQuery or other handy libraries on this page.
回答1:
You first need to access the iframe document, here is a cross-browser way to do it:
var ifr=document.getElementById('iframeID');
var doc=ifr.contentWindow||ifr.contentDocument;
if (doc.document) doc=doc.document;
You can then reach the iframe content:
doc.getElementById('child element ID')
Note: this will only work within a same domain, due to the same origin policy.
来源:https://stackoverflow.com/questions/16699105/finding-child-element-from-iframe-via-getelementbyid-or-other-technique