简体   繁体   English

为什么jquery不能在布局中定义的页面上工作

[英]Why jquery doesn't work on a page defined in layout

I have a razor layout and it loads css jquery etc. 我有一个razor布局,它加载css jquery等。
When I add controller action view I set layout to be _Layout.cshtml 当我添加控制器动作视图时,我将布局设置为_Layout.cshtml
But jquery doesn't work in that page. 但是jquery在该页面中不起作用。
So I have to add @Scripts.Render("~/bundles/jquery") in each view. 所以我必须在每个视图中添加@Scripts.Render("~/bundles/jquery")
Why imported jquery is not inherited from _Layout.cshtml ? 为什么导入的jquery不是从_Layout.cshtml继承的? Is it normal? 这是正常的吗?

This should be near the bottom of _Layout.cshtml: 这应该在_Layout.cshtml的底部附近:

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

This should be in your child view: 这应该在您的子视图中:

@section scripts {
    @Scripts.Render("~/scripts/jquery.infinitescroll.js") // Or some other script
}

All your scripts (that reference jquery) in your child views need to be in the scripts section, not placed in the body of the page. 您的子视图中的所有脚本(引用jquery)都需要位于脚本部分,而不是放在页面正文中。 That includes any script tags that want to make use of jquery as well. 这包括任何想要使用jquery的脚本标记。 For example, one of my pages has the following: 例如,我的一个页面具有以下内容:

@section scripts {
    @Scripts.Render("https://maps.googleapis.com/maps/api/js?key=&sensor=false")
    <script src="~/scripts/google-maps-3-vs-1-0.js"></script>
    <script>
        Stuff here
    </script>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM