我使用jquery,我看到目前,Jquery在每个内容页面中都有一个引用。我计划将所有引用移动到母版页,以便在需要时轻松更新它们。
因此,我从内容页中删除了jquery引用,并将它们放在母版页的head部分中,如下所示:
<head id="Head1" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="App_Themes/masterStyleSheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css"
<asp:ContentPlaceHolder ID="ExtraHeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
当我运行应用程序时,出现以下错误:
JavaScript runtime error: 'jQuery' is undefined
从我在网上的研究来看,这是正确的方法......但我得到了错误。任何人都可以帮助我并指出哪里出了问题或需要做什么?
将 jquery 脚本标记移到 jquery UI 脚本标记上方,并删除其中一个 jquery UI 引用,因为不需要包含它们两次:
<head id="Head1" runat="server">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link href="App_Themes/masterStyleSheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css"
<asp:ContentPlaceHolder ID="ExtraHeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
jQuery 未定义,因为 jquery UI 库在 jquery-1.9.1.js 文件中定义 jQuery 引用之前尝试使用 jQuery 引用。
首先,更改调用 jQuery UI 和 jQuery 库的顺序,所有使用 jQuery 的库或插件都需要在调用之前定义 jQuery,这是针对所有库或帧的
:<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
然后,以便您可以在代码中看到您正在调用两次 jQuery UI,请检查这一点,如果您调用两次 jQuery 或其他库:
http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js
http://code.jquery.com/ui/1.10.3/jquery-ui.js
加载jQuery库之前调用jQuery UI库
您的代码
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
将其更改为
首先调用 jQuery 库文件
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>