jquery - 无法在 Chrome 扩展中使用 JQuery

标签 jquery html google-chrome google-chrome-extension

我有一个 Google Chrome 扩展程序,其中有一个 background.js,我正在尝试在其中使用 JQuery。

出现以下错误。

Uncaught ReferenceError: $ is not defined

我的 list 文件代码部分

 "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["script.js", "jquery.js","front.js"]
    }
  ],
  "web_accessible_resources": ["https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"],
  "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",

我无法获得有关在此扩展中使用 JQuery 函数的任何线索。如果您需要更多理由,请告诉我。

编辑#1: list 文件

{
  "name": "Tool",
  "description": "Extension",
  "manifest_version": 2,
  "version": "5.0.0.0",
  "manifest_version": 2,
  "background": { "scripts": ["background.js"] },
  "permissions": [
    "tabs", "http://*/*", "https://*/*", "storage"
  ],
  "options_page": "options.html",
  "icons":{"16": "images/F_icon_16x16.png",
           "48": "images/F_icon_48x48.png",
          "128": "images/F_icon_128x128.png"},
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["jquery.js", "script.js", "front.js"]
    }
  ],
  "content_security_policy": "script-src 'self' ; object-src 'self'",
  "browser_action": {
      "default_title": "Tool",
      "default_icon": "images/F_icon.png"
  }
}

最佳答案

改变这部分:

"js": ["script.js", "jquery.js","front.js"]

为此:

"js": ["jquery.js","script.js","front.js"]

该行的顺序是包含文件的顺序。所以您首先需要 jquery.js 来定义 $(用于 jquery),然后使用您的脚本编写其他脚本...

关于jquery - 无法在 Chrome 扩展中使用 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702924/

相关文章:

javascript - 如何通过选择标题的一部分来为图像添加边框?

jquery - 无限轮播字幕修改

javascript - 具有月 View 和事件 View 的日期选择器?

html - jQuery 添加 css 到当前 li js

javascript - 根据属性覆盖<div>

html - Bootstrap 4 : how to change the order of a columns when the screen size changes

javascript - javascript sort() 顺序出现问题

javascript - NamedNodeMap 对象内的 Attr 对象索引问题

JavaScript 时区对于过去的夏令时转换规则是错误的

node.js - Jade 将空的 `style` 元素添加到 `head`