简体   繁体   English

jQuery准备就绪后,在_layout视图中,当页面加载时,会从子视图自动调用javascript函数

[英]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.

相关问题 数据准备就绪时调用javascript函数(在ERB视图中) - Invoke javascript function when data is ready (in ERB view) 在布局页面中添加document.ready之后,未找到包含document.ready函数的视图中的脚本标记 - Script tag in view containing document.ready function is not getting hit after adding document.ready in layout page 页面加载后,如果不在视图中,则在容器内自动滚动到活动水平元素 - Auto-scroll to active horizontal element when not in view, within a container, after the page loads 从HTML文件加载页面后,在“ .js”文件中调用JavaScript函数 - Invoke JavaScript function in “.js” file after a page loads from HTML file 当页面准备好时使用 jquery 将导航栏滚动到视图之外 - scroll navbar out of view when page is ready using jquery 从子页面调用父页面中的javascript函数 - Invoke javascript function in parent page from child page 页面加载时如何在我的视图模型中执行函数 - How to execute a function in my view model when page loads 如何使用Jquery在Javascript中加载页面时调用函数 - How to call a function when a page loads in Javascript with Jquery jQuery Mobile页面加载时运行Javascript函数 - Run Javascript function when jquery mobile page loads 在链接页面加载时,使用jQuery / Javascript从外部链接应用过滤器功能 - Using jQuery / Javascript to apply filter function from an external link when linked page loads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM