如何将 JavaScript 包含到页眉 MVC4 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12197323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to to include JavaScript into the page header MVC4
提问by Developer
Possible Duplicate:
ASP.Net MVC 3 Razor: Include js file in Head tag
I don't want to put a lots of JS into some layout and I need to do it for some specific pages I mean to include some of the JS into their header.
我不想将大量 JS 放入某些布局中,我需要为某些特定页面执行此操作,我的意思是将一些 JS 包含在其页眉中。
I've tried like that but it doesn't work as it should be.
我已经试过了,但它没有像它应该的那样工作。
@{
Layout = "~/Views/Shared/_LayoutInner.cshtml";
@Scripts.Render("~/Scripts/farbtastic/farbtastic.js")
@Styles.Render("~/Scripts/farbtastic/farbtastic.css")
@Scripts.Render("~/Scripts/jquery.tinycarousel.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.8.11.min.js")
}
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#slider1').tinycarousel();
$("#accordion").accordion();
$('#picker').farbtastic('#color');
});
</script>
I have tried like that
我试过这样
@{
Layout = "~/Views/Shared/_LayoutInner.cshtml";
<script type="text/javascript" src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
}
and no success at all.
根本没有成功。
How I can archive it?
我该如何存档?
回答by ssilas777
I'm sure in your _LayoutInner.cshtml
you should have refered JS files similarly like this
我确定_LayoutInner.cshtml
你应该像这样引用 JS 文件
<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
</head>
To achieve your target you have to add two named sections into your _LayoutInner.cshtml
pages head section like this-
为了实现您的目标,您必须像这样将两个命名部分添加到您的_LayoutInner.cshtml
页面头部部分中 -
<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
@RenderSection("JavaScript", required: false)
@RenderSection("CSS", required: false)
</head>
Now in your other pages to include extra javascript or css pages use these named sections
现在在您的其他页面中包含额外的 javascript 或 css 页面,请使用这些命名部分
@section JavaScript
{
<script type="text/javascript"src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
}
@section CSS
{
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
}
It is upto you whether to include different named sections for javascript and css.
是否为 javascript 和 css 包含不同的命名部分由您决定。
Hope it helps!
希望能帮助到你!