mvc4中,在含有母版页(_Layout)的项目中引用jquery插件

mvc4中,在含有母版页(_Layout)的项目中引用jquery插件,第1张

这个问题其实多半是因为你把JQ放在了下面,而你调用的函数在他前面

把一些基础的JS放在header里好一点,header里的JS是在BODY加载前优先加载的,写在body里的js好像不一定是按顺序加载的,有时候会不一样,具体我自己没测过。

所以要么把jq和基础的js放在header里,要么就是把页面要执行的js都写在页面加载完成事件里

$(function(){

...

})

View文件夹下面会有个_ViewStart.cshtml的文件,初始化了你的模板。如果不使用模板Layout = null

View Code

@{

Layout = "~/Views/Shared/_Layout.cshtml"

}

添加css可以按楼上说的

通常我会把CSS |RenderSection加在头,js加在末尾

View Code

复制代码

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

@RenderSection("Meta",false)

<title>@ViewBag.Title</title>

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>

@RenderSection("Css",false)

</head>

<body>

@RenderBody()

@RenderSection("Js",false)

</body>

</html>

复制代码

在视图直接引用

@section Css{

<link href="@Url.Content("~/Content/AdminCss/Article.css")" rel="Stylesheet" type="text/css" />

}

在布局页里添加以下代码。

<head>

    <title>@ViewBag.Title</title>

    <meta http-equiv="Content-Type" content="text/html charset=UTF-8" />

    <meta name="keywords" content="@ViewBag.Keywords" />

    <meta name="description" content="@ViewBag.Description" />

    @RenderSection("Head",false)

</head>

在每个页面添加以下代码

@{

    ViewBag.Title = Model.webSetting.Title

    ViewBag.Keywords = Model.webSetting.Keywords

    ViewBag.Description = Model.webSetting.Description 

    Layout = "~/Views/Shared/_Layout.cshtml"

}

@section Head

{

    <link href="/Content/css/index.css" rel="stylesheet" type="text/css" />

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/7888669.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存