To get the window object for a frame you can use the window.frames array:var iframewindow= frames['iframe_name'];这要求你给 一个老式的 name 属性而不是 id.或者,如果您知道页面上 iframe 的顺序,您可以用数字索引它们:This requires that you give the <iframe> an old-school name attribute instead-of-or-as-well-as the id. Alternatively if you know the order of iframes on the page you can index them numerically:var iframewindow= frames[0];从DOM中的iframe元素获取iframe窗口一般更灵活,但这需要一些兼容代码来应对IE:It's generally more flexible to get the iframe window from the iframe element in the DOM, but this requires some compatibility code to cope with IE:var iframe= document.getElementById('iframe_id');var iframewindow= iframe.contentWindow? iframe.contentWindow : iframe.contentDocument.defaultView;jQuery 定义了 contents() 方法来抓取 document节点,但它没有为您提供从 document 到 window 的跨浏览器方式,因此您仍然坚持:jQuery defines the contents() method to grab the document node, but it doesn't give you a cross-browser way to go from the document to the window, so you're still stuck with:var iframe= $('#iframe_id')[0];var iframewindow= iframe.contentWindow? iframe.contentWindow : iframe.contentDocument.defaultView;这并不是一个很大的胜利.which isn't really a big win.(注意:使用 jQuery 进行跨框架脚本编写时要非常小心.每个框架都需要自己的 jQuery 副本,一个框架副本中的方法不一定适用于另一个框架的节点.跨框架脚本是一种充满陷阱的话题.)(Note: be very careful using jQuery for cross-frame-scripting. Each frame needs its own copy of jQuery and methods from one frame's copy won't necessarily work on nodes from the other. Cross-frame-scripting is a topic fraught with traps.) 这篇关于如何使用 jQuery 公开 IFrame 的 DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 23:10