javascript - 如何在 chrome 扩展中包含 jquery 文件
问题描述
我想要使用 jquery 的 chrome 扩展中的复选框的值,但它给了我错误:
“$ 未定义”
我下载了 jquery 文件并保存在文件夹中,并在其中给出了 jquery 文件的路径,manifest.json
但是仍然会出现同样的错误。所以我想知道包含 jquery 文件的正确方法是什么。
我这样写:
{
"name": "Chrome Extension Test",
"version": "1.0",
"description": "Chrome extension to investigate Etsy product listings.",
"permissions": [
"storage",
],
"browser_action": {
"default_icon": {
"16": "images/get_started16.png"
},
"default_popup": "background.html",
"icons": {
"16": "images/get_started16.png"
},
"default_title": "Seller Tools"
},
"background": {
"scripts": [
"background.js", "js/jquery/jquery.js"
],
"persistent": false
},
"manifest_version": 2
}
我在 background.js 中编写的整个代码,但是当我为任何功能编写 Jquery(包括 $)时,它给了我“$ 未定义”的错误,我面临着非常困难。它也不允许我写。
解决方案
你做的几乎是正确的,你唯一需要修复的是脚本顺序。您在内容脚本中使用 jQuery,因此应首先加载 jQuery:
{
"name": "Chrome Extension Test",
"version": "1.0",
"description": "Chrome extension to investigate Etsy product listings.",
"permissions": [
"storage",
],
"browser_action": {
"default_icon": {
"16": "images/get_started16.png"
},
"default_popup": "background.html",
"icons": {
"16": "images/get_started16.png"
},
"default_title": "Seller Tools"
},
"background": {
"scripts": [
"js/jquery/jquery.js", "background.js"
],
"persistent": false
},
"manifest_version": 2
}
推荐阅读
- swift - 只需要更新 NSManagedObject 的最后一条记录
- perl - perl - 解析字节长度为 3,5,6,7 的有符号整数
- linux - 将带空格的字符串传递给 gcc
- windows - 验证本地 Windows 域的身份
- c# - 来自不同部署策略的网页中相同代码内的视觉差异
- vba - 使用 objSelection.EndKey 移动到 word doc 底部时出现错误参数
- javascript - Bootstrap Scrollspy 不适用于主干应用程序
- python - 安装/卸载 Tensorflow 的问题(无法)
- python - 当重新分配对自身的 Python 引用时,它是取消分配并重新分配,还是什么都不做?
- vb.net - DataTable.GetChanges() 未按预期工作