如何在Firefox插件上使用jQuery 1.5.2+? [英] How can I use jQuery 1.5.2+ on a Firefox addon?

查看:108
本文介绍了如何在Firefox插件上使用jQuery 1.5.2+?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

 函数getjQuery(window)$ b返回一个参数并返回jQuery的函数$ b {
/ * jquery code * /(window);
返回window.jQuery;
}

但是我收到了一封电子邮件,他们告诉我必须使用原始文件名和完全未修改的jQuery文件。



我开始搜索替代方法,找到这个解决方案,但是它没有办法工作。

< jQuery对象被创建,但我找不到任何元素。 $(#id)。length 总是 0

我当前的代码(这是行不通的)

  AddonNameSpace.jQueryAux = jQuery; 
$ b AddonNameSpace。$ = function(selector,context){
return // correct window
AddonNameSpace.jQueryAux.fn.init(selector,context || contentWindow);
};
AddonNameSpace。$。fn =
AddonNameSpace。$。prototype = AddonNameSpace.jQueryAux.fn;
AddonNameSpace.jQuery = AddonNameSpace。$;

jQuery文件正在我的 browser.xul overlay:

 < script type =text / javascriptsrc =chrome:// addon / content / bin /jquery-1.5.2.min.js/> 

我在正确的地方加载吗?

<如何使用jQuery修改原始jQuery文件的页面内容(HTML),甚至有可能? 解决方案

您需要在jquery的第二个参数上传递e.originalTarget.defaultView。
如果您不使用jQuery,将使用window.document,这是来自xul的window.document。


$ b

使用

  gBrowser.addEventListener(DOMContentLoaded,function(e){
$(#id,e.originalTarget.defaultView).length
},true);

而不是

  $( #标识)的长度。 

而且,为避免与其他扩展冲突,请勿在xul页面中使用脚本,请使用MozIJSSubScriptLoader。

  Components.classes [@ mozilla.org/moz/jssubscript-loader;1] 
.getService Components.interfaces.mozIJSSubScriptLoader)
.loadSubScript(chrome://youraddon/content/jquery-1.5.2.min.js);

如果使用此方法,则只在需要时才加载jquery,避免内存泄漏。 >

At first I made a function that received a parameter and returned jQuery such as:

function getjQuery(window)
{
   /*jquery code*/(window);
   return window.jQuery;
}

But then I got an email form the review and they told me I have to use jQuery file with the original file name and completely unmodified.

I started to search for an alternative and found this solution, but there is no way it work.

jQuery object is created, but I can't find any elements. $("#id").length is always 0. With the previous method it was always found.

My current code (which doesn't work)

AddonNameSpace.jQueryAux = jQuery;

AddonNameSpace.$ = function(selector,context) { 
    return                                                 // correct window
        new AddonNameSpace.jQueryAux.fn.init(selector,context||contentWindow); 
};
AddonNameSpace.$.fn =
    AddonNameSpace.$.prototype = AddonNameSpace.jQueryAux.fn;
AddonNameSpace.jQuery = AddonNameSpace.$;

The jQuery file is loading on my browser.xul overlay:

<script type="text/javascript" src="chrome://addon/content/bin/jquery-1.5.2.min.js" />

Am I loading in the right place?

How can I use jQuery to modify the content on a page (HTML) with the original jQuery file, is it even possible?

解决方案

You need pass the e.originalTarget.defaultView on the second parameter on jquery.. If you don't jquery will use window.document, which is the window.document from the xul.

Use

gBrowser.addEventListener("DOMContentLoaded", function (e) {
    $("#id", e.originalTarget.defaultView).length
}, true);

instead of

$("#id").length;

And, for avoid conflicts with other extensions don't use script in the xul page, use MozIJSSubScriptLoader.

Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader)
.loadSubScript("chrome://youraddon/content/jquery-1.5.2.min.js"); 

If you use this method, you load jquery only when you need, avoiding memory leak.

这篇关于如何在Firefox插件上使用jQuery 1.5.2+?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆