JQuery的身体ASP .NET MVC 4后 [英] JQuery After Body ASP .NET MVC 4

查看:176
本文介绍了JQuery的身体ASP .NET MVC 4后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建一个ASP .NET MVC 4的WebAPI项目。

I just created an ASP .NET MVC 4 WebAPI project.

在看默认_Layout.cshtml,我看到身体之后被渲染(不是在头)被插入的jQuery脚本。

In looking at the default _Layout.cshtml, I see that the jquery script is being inserted after the body is rendered (not in the head).

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/themes/base/css", "~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

这将导致错误

$ is not defined 

当然,尝试写

$(document).ready(function() {...}).

移动

@Scripts.Render("~/bundles/jquery")

到_Layout.cshtml的头部分纠正问题和jQuery按预期工作。

into the head section of the _Layout.cshtml corrects the problem and jquery works as expected.

这是怎么回事?难道我做错了什么,并没有对里面的_Layout.cshtml的Script.Render默认位置理由吗?

What's up? Am I doing something wrong and there's a reason for the default location of the Script.Render inside _Layout.cshtml?

推荐答案

脚本加载和执行模块页面的渲染。

Script loading and executing block the rendering of the page.

您可以通过在你的页面的多个部分添加警报看到这一点。这就是为什么我们建议把js文件在页面的背面。

You can see this by adding alerts in multiple parts of your page. That's why it's recommended to put the js files at the back of the page.

您还可以通过拥有一个在此加入jQuery的背后网页中的脚本一节,在页面的最后解决您的问题。

You can also remedy your problem by having a section for page scripts behind the one where jquery is added, at the end of the page.

编辑:下面是从斯科特·格思里约在剃刀部分的一篇文章:<一href=\"http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx\">http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Here's an article from Scott Guthrie about sections in razor: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

这篇关于JQuery的身体ASP .NET MVC 4后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆