繁体   English   中英

ASP.NET中外部javascript文件中的jQuery

[英]jQuery in external javascript file in ASP.NET

如何在ASP.NET的外部javascript文件中使用jQuery?

这是我的代码:

<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>

但是在Javascript.js文件中,我不能使用jQuery选择器$() 当我使用它不起作用。

Javascript.js

$(document).ready(function () { 
  alert('Hello'); 
});

此代码无效。 同样,IntelliSense在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>

您说过在开发人员控制台中有以下错误:

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

您可以通过正确设置文件路径来解决这些问题:

<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>

通过将以下代码行放入Javascript.js(使自己意识到自己),使IntelliSense起作用:

/// <reference path="jquery-2.1.1.js" />

在此处阅读IntelliSense功能和参考指令的文档: http : //msdn.microsoft.com/zh-cn/library/bb385682.aspx#Features

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM