Firefox的扩展,jQuery和访问文档 [英] firefox extension, jquery and accessing the document
问题描述
当我使用content.document在扩展中的pageLoad函数内部时,我可以轻松地遍历当前窗口,但是当我尝试在另一个函数中使用setInterval调用时,我无法访问content.document。
我尝试了content.document,document.defaultView,window.content.document和基本上所有我能想到的明智的组合。
请帮忙。感谢。
我得到这个工作是通过将文档对象从事件中取出来的。
$ b
e.target.defaultView.document
事件对象可用于任何事件侦听器函数 - 例如你可能会通过这样的方式捕获文档加载的事件:
$ b $ $ p $ var appcontent = window.document.getElementById('appcontent' );
appcontent.addEventListener('DOMContentLoaded',onPageLoad,false);
在这个例子中,onPageLoad函数获取一个事件参数,可以用来获取文档。 / p>
要能够访问其他地方的文档对象,请设置一个指向该文档对象的全局变量。如果使用jQuery,则将其作为可选的第二个参数传递给$函数(即jQuery工作的上下文)。
I can easily traverse the current window while I'm inside the pageLoad function in my extension using content.document, but when I try accessing it in another function, called with setInterval, I can't access the content.document.
I tried content.document, document.defaultView, window.content.document and basically every other sensible combination I could think of.
please help. thanks.
I got this to work by getting the document object out of an event like so:
e.target.defaultView.document
The event object is available in any event listener function - for e.g. you may be capturing the document loaded event by doing something like this:
var appcontent = window.document.getElementById('appcontent');
appcontent.addEventListener('DOMContentLoaded', onPageLoad, false);
In this example, the onPageLoad function gets an event argument that can be used to fetch the document.
To be able to access the document object elsewhere, set a global variable pointing to that. And if using jQuery, pass this as the optional second argument to the $ function (i.e. the context on which jQuery works).
这篇关于Firefox的扩展,jQuery和访问文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!