首页 > 解决方案 > 如何在 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(包括 $)时,它给了我“$ 未定义”的错误,我面临着非常困难。它也不允许我写。

标签: javascriptjquery

解决方案


你做的几乎是正确的,你唯一需要修复的是脚本顺序。您在内容脚本中使用 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

}

推荐阅读