为什么jquery不能在布局中定义的页面上工作 [英] Why jquery doesn't work on a page defined in layout
问题描述
我有一个 razor
布局,它加载 css jquery等。
当我添加控制器操作视图设置布局为 _Layout.cshtml
但是jquery在该页面中不起作用。
所以我必须在每个视图中添加 @ Scripts.Render(〜/ bundles / jquery)
。
为什么导入jquery不会继承 _Layout.cshtml
?是否正常?
I have a razor
layout and it loads css jquery etc.
When I add controller action view I set layout to be _Layout.cshtml
But jquery doesn't work in that page.
So I have to add @Scripts.Render("~/bundles/jquery")
in each view.
Why imported jquery is not inherited from _Layout.cshtml
? Is it normal?
推荐答案
这应该在_Layout.cshtml底部:
This should be near the bottom of _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
}
您子视图中的所有脚本(引用jquery)需要在脚本部分,而不是放在页面的主体中。这包括任何想要使用jquery的脚本标签。例如,我的某个网页包含以下内容:
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. That includes any script tags that want to make use of jquery as well. 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>
}
这篇关于为什么jquery不能在布局中定义的页面上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!