简体   繁体   English

如何在chrome扩展名中包含jquery文件

[英]how to include the jquery file in chrome extension

I want the value of checkbox in chrome extension using jquery, but it is giving me error: 我想要使​​用jquery在chrome扩展中的checkbox的值,但这给了我错误:

"$ is not defined" “未定义$”

I download the jquery file and save in folder and giving path of jquery file in manifest.json but, still same error is coming. 我下载了jquery文件并保存在文件夹中,并在manifest.json提供了jquery文件的路径,但是仍然出现相同的错误。 So I want to know what is correct way to include jquery file. 所以我想知道包括jquery文件的正确方法是什么。

I write like this : 我这样写:

{
    "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

}

The whole code I am writing in background.js, but when I write Jquery(including $) for any functionality so it's giving me error that "$ is not defined" and I am facing very difficult. 整个代码是我在background.js中编写的,但是当我为任何功能编写Jquery(包括$)时,它给我的错误是“未定义$”,并且我面临着非常困难。 It is also not allowing me to write . 这也不允许我写。

You are doing it almost correctly, the only thing you need to fix is the script order. 您几乎可以正确执行此操作,只需修复脚本顺序即可。 You are using jQuery in your content script, so jQuery should be loaded first: 您正在内容脚本中使用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

}

You can include it in the head tag of your HTML document and not in the .js or .json file 您可以将其包含在HTML文档的head标签中,而不要包含在.js或.json文件中

<head>
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
</head>

Then you can use jquery any where in your HTML document 然后,您可以在HTML文档中的任何位置使用jquery

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

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