[英]jQuery in external javascript file in ASP.NET
How to use jQuery in external javascript file in ASP.NET? 如何在ASP.NET的外部javascript文件中使用jQuery?
This is my code : 这是我的代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="~/Javascript/Javascript.js" runat="server"></script>
<script src="~/Javascript/jquery-2.1.1.js"></script>
</head>
But in Javascript.js
file, I can't use jQuery selector $()
. 但是在Javascript.js
文件中,我不能使用jQuery选择器$()
。 When I use it doesn't work. 当我使用它不起作用。
Javascript.js : Javascript.js :
$(document).ready(function () {
alert('Hello');
});
This code doesn't work. 此代码无效。 Also IntelliSense doesn't work in Javascript.js. 同样,IntelliSense在Javascript.js中也不起作用。
You have to include jQuery before Javascript.js 您必须在Javascript.js之前包含jQuery
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="~/Javascript/jquery-2.1.1.js"></script>
<script src="~/Javascript/Javascript.js" runat="server"></script>
</head>
You said you have these errors in your developer console: 您说过在开发人员控制台中有以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found) localhost:46316/~/Javascript/Javascript.js
Failed to load resource: the server responded with a status of 404 (Not Found) localhost:46316/~/Javascript/jquery-2.1.1.js
Uncaught TypeError: undefined is not a function
You can fix these by setting the paths to your files correctly: 您可以通过正确设置文件路径来解决这些问题:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="/Javascript/jquery-2.1.1.js"></script>
<script src="/Javascript/Javascript.js" runat="server"></script>
</head>
Make IntelliSense work by putting this line of code to Javascript.js (as you realized yourself): 通过将以下代码行放入Javascript.js(使自己意识到自己),使IntelliSense起作用:
/// <reference path="jquery-2.1.1.js" />
Read the documentation of IntelliSense features and reference directives in here: http://msdn.microsoft.com/en-us/library/bb385682.aspx#Features 在此处阅读IntelliSense功能和参考指令的文档: http : //msdn.microsoft.com/zh-cn/library/bb385682.aspx#Features
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.