外部javascript文件中无法识别jQuery
我试图将html中的jquery代码表单脚本标记移动到单独的javascript文件中,但新文件中没有完整的代码,$变量不能识别为jquery。但代码运行良好。 在visual studio中没有完整代码的my index.js文件中:外部javascript文件中无法识别jQuery,javascript,jquery,html,Javascript,Jquery,Html,我试图将html中的jquery代码表单脚本标记移动到单独的javascript文件中,但新文件中没有完整的代码,$变量不能识别为jquery。但代码运行良好。 在visual studio中没有完整代码的my index.js文件中: $( function(){ $("a").attr({href:"http://reddit.com"}).text("reddit.com") $(".header").css("color","red"); } );
$( 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
打开开发者工具控制台。你看到错误了吗?