我想在Firefox扩展中使用jQuery,我将库导入到xul文件中,如下所示:
<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>
但是xul文件中无法识别$()函数,jQuery()也无法识别。
我还尝试过将“ content.document”对象(该对象反映“ document”对象)作为上下文参数传递给jQuery函数,如下所示:
$('img',content.document);
但仍然无法正常工作,以前有人遇到过这个问题吗?
我使用以下内容example.xul:
example.xul
<?xml version="1.0"?> <overlay id="example" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <head></head> <script type="application/x-javascript" src="jquery.js"></script> <script type="application/x-javascript" src="example.js"></script> </overlay>
这是一个 example.js
example.js
(function() { jQuery.noConflict(); $ = function(selector,context) { return new jQuery.fn.init(selector,context||example.doc); }; $.fn = $.prototype = jQuery.fn; example = new function(){}; example.log = function() { Firebug.Console.logFormatted(arguments,null,"log"); }; example.run = function(doc,aEvent) { // Check for website if (!doc.location.href.match(/^http:\/\/(.*\.)?stackoverflow\.com(\/.*)?$/i)) return; // Check if already loaded if (doc.getElementById("plugin-example")) return; // Setup this.win = aEvent.target.defaultView.wrappedJSObject; this.doc = doc; // Hello World this.main = main = $('<div id="plugin-example">').appendTo(doc.body).html('Example Loaded!'); main.css({ background:'#FFF',color:'#000',position:'absolute',top:0,left:0,padding:8 }); main.html(main.html() + ' - jQuery <b>' + $.fn.jquery + '</b>'); }; // 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); })();