iFrame中的Firebug和jQuery选择器 [英] Firebug and jQuery selectors in an iFrame
问题描述
我正在开发一个Web应用程序并使用jQuery插件Colorbox弹出一个窗口,该窗口显示一个用于编辑父窗口元素的表单。我正在使用Firebug来调试我的Javascript和jQuery,我注意到我无法使用jQuery控制台命令行在Colorbox HTML表单中选择一个元素。例如:
I'm working on a web application and using the jQuery plug-in Colorbox to pop up a window that presents a form for editing elements of the parent window. I'm using Firebug to debug my Javascript and jQuery, and I noticed that I can't select an element in my Colorbox HTML form using the jQuery console command line. For instance:
$ date = $(#date);
$date = $("#date");
从运行时返回任何内容jQuery控制台命令行,即使我有一个id =date的输入元素,并且Firebugelement inspect指针可以在iFrame中找到该元素。有没有办法让Firebug的控制台访问iFrame中的元素?
returns nothing when run from the jQuery console command line, even though I have an input element with id="date" and the Firebug "element inspect" pointer can find the element in the iFrame. Is there a way to get Firebug's console to access the elements in an iFrame?
感谢您的帮助!
Doug
Thanks for your help! Doug
推荐答案
您需要将iframe的文档作为选择器的上下文传递,因为iframe有自己独立的DOM树:
You need to pass iframe's document as a context for your selector, because iframe has its own separated DOM tree:
$('#date', $('iframe').get(0).contentDocument);
为了获得iframe内容的访问权限,必须从父文档的同一域加载。
In order to get access in iframe's content, it must be loaded from same domain as parent document.
这里只详细说明, .contentDocument
属性在Chrome中有效(和FF)但不是IE< 8。你必须使用 .document
。
Just to elaborate here, .contentDocument
property works in Chrome (and FF) but not in IE<8. You have to use .document
.
这篇关于iFrame中的Firebug和jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!