Finding child element from iFrame via getElementById or other technique?

核能气质少年 提交于 2019-12-25 04:10:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!