简体   繁体   English

javascript(greasemonkey)如何访问新创建的iframe

[英]javascript (greasemonkey) how to access a newly created iframe

i have this website, by clicking on certain link on the website an iframe will be created within the same window. 我有这个网站,通过单击网站上的某些链接,将在同一窗口内创建iframe。

my question, just wondering how to access this iframe? 我的问题,只是想知道如何访问此iframe? i tried document.getElementById() return null tried window.frames not there 我尝试了document.getElementById()返回null尝试过的window.frames不存在

Thanks 谢谢

If you've given the <iframe> element an ID then document.getElementById() will work. 如果为<iframe>元素指定了ID,则document.getElementById()将起作用。 If you're trying to get hold of its window object then you'll need to use the iframe element's contentWindow property (or use the more standard contentDocument , which is a reference to the iframe's document object but is sadly absent from IE, at least up to and including version 7): 如果您试图掌握其window对象,则需要使用iframe元素的contentWindow属性(或使用更标准的contentDocument ,它是对iframe document对象的引用,但遗憾的是,IE至少没有此内容)直到并包括版本7):

var iframe = document.getElementById("your_iframe_id");
var iframeWin, iframeDoc;
if (iframe.contentDocument) {
    iframeDoc = iframe.contentDocument;
    iframeWin = iframeDoc.defaultView;
} else if (iframe.contentWindow) {
    iframeWin = iframe.contentWindow;
    iframeDoc = iframeWin.document;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM