本文介绍了无法在Chrome扩展中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚为什么我不能在我的扩展中使用jQuery,我是一个绝对的初学者,但理论上这应该完成这项工作:
清单
{
manifest_version:2,
name :iD,
version:0.1,
description:iD,
browser_action:{
//default_icon: icon.png,
default_popup:popup.html,
default_title:iD
},
content_scripts:[{
js:[jquery.min.js,app.js],
matches:[http:// * / *,https:// * / *]
}]
}
popup.html
<!DOCTYPE html>
< html>
< head>
< title>< / title>
< / head>
< body>
< / body>
< / html>
app.js
$(body)。append(Hello World);
然而我所看到的只是一个空的弹出窗口而不是Hello World
解决方案
您无法注入到扩展页面(包括弹出窗口)。
您需要:
-
阅读。
-
将脚本直接添加到弹出窗口中:
< script src =jquery。 min.js>< /脚本>
< script src =app.js>< / script>
-
(该评论提出了一个有效的观点)操作,将代码封装在:
$(document).ready(function(){
/ *在这里操作DOM * /
});
I am trying to figure out why I am not able to use jQuery in my extension, I am an absolute beginner, but in theory this should do the job:
manifest
{
"manifest_version": 2,
"name" : "iD",
"version" : "0.1",
"description" : "iD",
"browser_action" : {
// "default_icon" : "icon.png",
"default_popup" : "popup.html",
"default_title" : "iD"
},
"content_scripts": [ {
"js": [ "jquery.min.js", "app.js" ],
"matches": [ "http://*/*", "https://*/*"]
}]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
app.js
$("body").append("Hello World");
Yet all I see is an empty popup instead of "Hello World"
解决方案
You can't inject content scripts into extension pages (popup included).
You need to:
Read the Architecture Overview.
Add the scripts directly to your popup:
<script src="jquery.min.js"></script> <script src="app.js"></script>
(the comment raises a valid point) For all DOM manipulation, wrap your code in the
ready()
event:$(document).ready(function() { /* Manipulate DOM here */ });
这篇关于无法在Chrome扩展中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!