繁体   English   中英

如何从文档对象访问窗口对象

[英]How to access the window object from the document object

是否可以直接从Javascript中的文档对象访问窗口对象?

例如:

// window.frames[0] returns the document object of the first frame or iframe found on the page
myFunc(window.frames[0]);

function myFunc(doc) {
  // I want to do something along these lines:
  var wnd = doc.getWindow();
  alert("Found frame: " + wnd.name);
  for (var i=0; i<wnd.frames.length; i++) {
    myFunc(wnd.frames[i]);
  }
}

抱歉,我无法使用jQuery。

根据MDN 文档,您应该已经使用window.frames[0]获取了该窗口。 如果您想要实际的文档,则需要获取实际的框架元素并深入文档中。

var firstFrame = document.getElementsByTagName( "iframe" )[ 0 ];
firstFrame.contentWindow;  // The window
firstFrame.contentWindow.document;  // The document

注意:我相信Safari的早期版本(3.0之前的IIRC)不支持contentWindow

暂无
暂无

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

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