【发布时间】:2014-05-20 22:29:29
【问题描述】:
我正在开发一个基于 jQuery 的 Firefox,如Answer here 中所述。
在实现答案中提供的示例后,一切正常,但问题是 Firefox 选项卡之间的代码以某种方式链接,example.doc 总是指最后打开的选项卡。
- 打开的 tab1:
plugin-example已添加到当前页面。 -
this.doc指的是tab1。 - 已打开 tab2:
plugin-example已添加到当前页面 (tab2)。 -
this.doc现在指的是 tab2 - 返回查看 tab1:
this.doc仍然指的是 tab1。 - 点击 tab1 上的
plugin-example将作用于 tab2 中的plugin-example。
如何使我的代码在选项卡之间独立?
以下是代码摘录:
(function() {
jQuery.noConflict();
$ = function(selector,context) {
return new jQuery.fn.init(selector,context||example.doc);
};
$.fn = $.prototype = jQuery.fn;
example = new function(){};
example.run = function(doc,aEvent) {
if (doc.getElementById("plugin-example")) return;
this.doc = doc;
this.main = main = $('<div id="plugin-example">').appendTo(doc.body).html('Example Loaded!');
this.main.click(function() { //<--- added this function
example.main.html(example.doc.location.href);
});
main.css({
background:'#FFF',color:'#000',position:'absolute',top:0,left:0,padding:8
});
};
// Bind Plugin
var delay = function(aEvent) {
var doc = aEvent.originalTarget; setTimeout(function() {
example.run(doc,aEvent);
}, 1);
};
var load = function() {
gBrowser.addEventListener("DOMContentLoaded", delay, true);
};
window.addEventListener("pageshow", load, false);
})();
【问题讨论】:
标签: javascript jquery firefox firefox-addon