在那里把脚本MVC _layout页 [英] Where to put scripts in MVC _layout page
问题描述
您好,我从本网站的脚本应该在_layout页面底部的阅读理解的建议。我的问题是我不知道确切位置的'底部'的。有些人说这只是一个标记之前但是,这并不为我工作。我试图把剧本在很多地方却无处似乎工作。可能有人请告诉我,我做错了。
这是我的关于页
@ {
ViewBag.Title =关于我们;
}< H2>关于< / H>
&所述p为H.;
把内容在这里。
&所述; / P><脚本类型=文/ JavaScript的>
$(函数(){
警报(的Hello World);
});
< / SCRIPT>
这是我的_layout页
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<标题> @ ViewBag.Title< /标题>
<链接HREF =@ Url.Content(〜/内容/的site.css)的rel =stylesheet属性类型=文/ CSS/>
@ * jQuery的工作,如果这条线是在这里但如果它是在底部
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.6.2.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
* @
< /头>
<身体GT;
< DIV CLASS =页>
< DIV ID =头>
< DIV ID =标题>
< H1>
我的MVC应用程序< / H1>
< / DIV>
< DIV ID =logindisplay>
@ Html.Partial(_ LogOnPartial)
< / DIV>
< DIV ID =MenuContainer中>
< UL ID =菜单>
<立GT; @ Html.ActionLink(家,索引,家庭)< /李>
<立GT; @ Html.ActionLink(关于,关于,家庭)< /李>
< / UL>
< / DIV>
< / DIV>
< DIV ID =主>
@RenderBody()
< / DIV>
< DIV ID =页脚>
< / DIV>
< / DIV>
@ *如果脚本是在这里jQery不工作! * @
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.6.2.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
< /身体GT;
< / HTML>
jQuery的文件有之前的文件中找到第一个引用的jQuery包括在内。因此,要解决这个问题,我把头部jQuery的文件,未在视图,但在其他脚本文件使用关闭之前都包含所有其他脚本主体标签
Hello I understand from reading advice on this site that scripts should go at the bottom of the _layout page. My problem is that I am not sure exactly where the 'bottom' is. Some people have said it is just before the tag but that does not work for me. I have tried putting the script in many places but nowhere seems to work. Could someone please tell me what I am doing wrong.
This is my About page
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
<script type="text/javascript">
$(function () {
alert("Hello World");
});
</script>
This is my _Layout page
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
@* jQuery works if this line is here but not if it is at the bottom
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
*@
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>
My MVC Application</h1>
</div>
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<div id="menucontainer">
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
</div>
<div id="main">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
@* jQery does not work if the script is here! *@
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
</body>
</html>
Jquery file has to be included before first reference to jquery is found on the document. So, to solve this problem, i put jquery file in head section and all other script that are not used in views but in other script files are included before closing body tag
这篇关于在那里把脚本MVC _layout页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!