简体   繁体   English

在Chrome扩展程序中运行JQuery

[英]Running JQuery in Chrome Extension

My html file: 我的html文件:

<html><body>example !</body></html>

My manifest.json 我的manifest.json

    {
  "name": "Test",
  "version": "0.1",
  "description": "Test",
  "content_scripts": [
    {
      "matches": ["http://*/*","file://*/*"],
      "run_at": "document_end",
      "js": ["myscript.js","jquery-1.10.2.min.js"]
    }
  ],
  "manifest_version": 2
}

myscript.js file: myscript.js文件:

/*var divtest = document.createElement("div");
    divtest.innerHTML = "new div";
    divtest.id = "divTest";
    document.body.appendChild(divtest);*/

$("body").append("Test");

The JS commented-out code works. JS注释掉的代码有效。 The JQuery on the other hand does nothing. 另一方面,JQuery什么都不做。

The problem is that you are trying to run your script before jQuery is initialized, so by the time you run $("body") - $ is not "jQuery" yet. 问题是你在jQuery初始化之前尝试运行你的脚本,所以当你运行$("body") - $还不是“jQuery”。 You need to load jQuery first: 你需要先加载jQuery:

{
  "name": "Test",
  "version": "0.1",
  "description": "Test",
  "content_scripts": [
    {
      "matches": ["http://*/*","file://*/*"],
      "run_at": "document_end",
      "js": ["jquery-1.10.2.min.js", "myscript.js"]
    }
  ],
  "manifest_version": 2
}

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

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