我试图将我的 jquery 代码表单脚本标记移到 html 中以分隔 javascript 文件,但新文件中没有完整的代码,并且 $ 变量不被识别为 jQuery。不过代码运行得很好。 在我的 index.js 文件中,该文件在 Visual Studio 中没有完整的代码:
$( function(){
$("a").attr({href:"http://reddit.com"}).text("reddit.com")
$(".header").css("color","red");
}
);
在我的html文件中,我在index.js之前加载了jquery,其中包含jquery代码:
<html>
<head>
<link rel="stylesheet" href="/static/css/style.css">
<script type="text/javascript" src="/static/js/jquery.js"></script>
<script type="text/javascript" src="/static/js/index.js"></script>
<title>Welcome</title>
<h1 class="header">Welcome to the test site</h1>
<a href = "http://www.google.com">Google.com</a>
</head>
<body>
<input type="button" onclick="location.href='/login';" value="Login" />
</body>
</html>
有谁知道为什么我的index.js无法识别jQuery库并将$分配给jquery变量? 谢谢,如果我需要澄清任何事情,请告诉我。
最佳答案
好吧,我所需要做的就是使用 npm 安装:
npm install --save @types/jquery
关于javascript - 外部 javascript 文件无法识别 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333542/