[英]auto invoke a javascript function from the child view, after jquery ready, in _layout view, when page loads
strong text In my ASP MVC 5 app, I have a (master - Jquery loads here ) _layout.cshtml
and (child, my function sortable here, not loading ) views tableView.cshtml
坚固的文本在我的ASP MVC 5应用程序中,我有一个(master- Jquery在这里加载 )
_layout.cshtml
和(子_layout.cshtml
,我的函数在这里可排序,而不是加载 )视图tableView.cshtml
In the child tableView.cshtml, I've written custom JS functions that need to be invoked on Jquery Read $ ready. 在子 tableView.cshtml中,我编写了需要在Jquery上调用的自定义JS函数。Read $ ready。 Since jquery has already loaded in the master page, how can I attach my function (and 3rd parts plugins) invoke it when my child page loads?
由于jquery已经加载到母版页中,因此如何在子页加载时附加我的函数(和第3部分插件)?
if possible, please share a modular way to attach and initialize my functions and 3rd party plugins in the childviews on the child view loading/navigation load, so that when the main jquery function loads, it also invokes my functions. 如果可能的话,请在子视图加载/导航加载时共享一种模块化方式,以在子视图中附加并初始化我的函数和第三方插件, 以便在加载主jquery函数时,它也调用我的函数。
_layout.cshtml _layout.cshtml
// DOM ready is completed in master layout, I have custom JS plugin/code (sortable)
// in the child view that I need to load, when that loads
$( document ).ready(function() {
console.log( "Master layout ready, done" );
});
TableView.cshtml TableView.cshtml
// in my tableView, that inherits layout from master,
// how can I get this loaded when the page loads
(function() {
console.log( "How can I get child table plugin, loaded!" );
})();
You should make use of @RenderSection()
which act as placeholders to render content from the view 您应该使用
@RenderSection()
作为占位符以从视图中呈现内容
You layout page might look something like 您的版面设计页面可能看起来像
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
.....
// Include common style sheets here
@RenderSection("styles", false) // placeholder for styles
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
....
@RenderBody()
....
// Include all common scripts here
@Scripts.Render("~/bundles/jquery") // include jquery first
@RenderSection("scripts", required: false) // placeholder for page specific scripts
</body>
</html>
and in the view 并认为
@model YourModel
// html here
....
@section styles {
<link href="~/Content/PageSpecificStyleSheet.css" rel="stylesheet" />
}
@section scripts {
// Add page specific scripts and plugin files here
@Scripts.Render("~/bundles/jqueryval")
<script src="../../Scripts/MyScript.js" type="text/javascript"></script>
....
<script type="text/javascript">
// Other javascript code here
</script>
}
Note @RenderSection("styles", false)
is in the <head>
element and @RenderSection("scripts", required: false)
is immediately before the closing </body>
tag meaning any scripts defined in the view will be loaded after the page elements have loaded (and after the jquery file) 注意
@RenderSection("styles", false)
位于<head>
元素中,而@RenderSection("scripts", required: false)
位于</body>
标记的紧前面,这意味着视图中定义的所有脚本都将在之后加载。页面元素已加载(并在jquery文件之后)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.