我有一个使用Details页面和Master页面的MVC2项目。当Details页面第一次运行时,一切都很棒 - 所有的javascript和jquery都运行得很好。但是,当分页运行时(使用PaginatedList和NerdDinner教程),第二页不会加载jquery对象。 (JQuery =“undefined”)。
我不知道为什么第二页不起作用。我已经将解决方案减少到以下最小代码以显示行为:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
<script src="../../Scripts/jquery-1.4.2.min.js"
type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
$(function () {
alert('MasterPage');
});
</script>
<%-- scripts from child pages get inserted here --%>
<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
</head>
<body>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</body>
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<WidgetViewModel>" %>
<asp:Content ID="TitleContent"
ContentPlaceHolderID="TitleContent"
runat="server">
The Title
</asp:Content>
<asp:Content ID="Content3"
ContentPlaceHolderID="ScriptContent"
runat="server">
<script type="text/javascript" language="javascript">
alert('DetailsPage');
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%-- No Content --%>
</asp:Content>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
The Title
</title>
<script src="../../Scripts/jquery-1.4.2.min.js"
type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
$(function () {
alert('MasterPage');
});
</script>
<script type="text/javascript" language="javascript">
alert('DetailsPage');
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
The Title
</title>
<script src="../../Scripts/jquery-1.4.2.min.js"
type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
$(function () {
alert('MasterPage');
});
</script>
<script type="text/javascript" language="javascript">
alert('DetailsPage');
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>
答案 0 :(得分:0)
我同意@Josh的评论,因为根据你的路线相对路径会发生变化......让它绝对,或者更好地使用jQuery的CDN版本:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"
type="text/javascript" language="javascript"></script>