繁体   English   中英

在js文件中包含jQuery

[英]include jquery in js file

我正在开发chrome扩展程序,它是一个简单的扩展程序,它调用background.js文件来完成简单的工作。 在background.js中,我想包含jquery.js文件来调用ajax函数。 但是我仍然找不到在background.js中包含jquery.js的内容。 可以这样做吗?

你知道,扩展名为background.js,它不调用包含background.js的php或html文件,所以我不能使用任何涉及php或html的东西

提前致谢....

听起来您正在使用manifest_version 2,这意味着您可以将path-to-jquery.js添加到background.scripts数组中。

"background": {
    "scripts": ["jquery.js", "background.js"]
}

这些文件全部一起加载到生成的页面中,因此它们访问相同的全局范围-这意味着您可以照常使用jQuery

你去:

var jQueryElement=document.createElement('script');
jQueryElement.type = 'text/javascript';
jQueryElement.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(jQueryElement);

您将必须将所有内容放入onload事件处理程序中,否则,即使尚未加载jQuery,其余的JavaScript也将继续运行。

var jQueryElement=document.createElement('script');
jQueryElement.type = 'text/javascript';
jQueryElement.onload = function () {
    alert('jQuery loaded');
};
jQueryElement.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(jQueryElement);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM