我是 JavaScript 和 Html 的初学者。目前我正在学习制作 chrome 扩展。
我的manifest.json文件是这样的:
{
"manifest_version": 2,
"name": "Hello World",
"version": "1.0",
"description": "A Hello world extensions",
"icons": {
"128": "128chat.png",
"48": "48chat.png",
"16": "16chat.png"
},
"browser_action": {
"default_icon": "16chat.png",
"default_popup": "popup.html"
}
}
我的 popup.html 文件是:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
<script src="popup.js"></script>
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<h1 id="greet">Hello</h1>
<input type="text" id="name">
</body>
</html>
我的 popup.js 文件是:
$(function () {
$('#name').keyup(function () {
$('#greet').text("hello " + $('#name').val());
});
});
我在我的项目中使用“jquery-3.5.1.min.js”jquery 文件。
但是在运行扩展时,我收到“ Uncaught ReferenceError :$未定义”
你能告诉我我的代码有什么问题吗?
最佳答案
HTML 页面从头到尾按顺序进行解析。
因此,在您的代码中,popup.js 文件在 jquery-3.5.1.min.js 之前加载。
您应该首先加载 jquery-3.5.1.min.js,然后加载 popup.js。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
<script src="jquery-3.5.1.min.js"></script>
<script src="popup.js"></script>
</head>
<body>
<h1 id="greet">Hello</h1>
<input type="text" id="name">
</body>
</html>
希望这对您有帮助。
关于javascript - 未捕获的引用错误 : $ is not defined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62099220/