簡體   English   中英

jQuery准備就緒后,在_layout視圖中,當頁面加載時,會從子視圖自動調用javascript函數

[英]auto invoke a javascript function from the child view, after jquery ready, in _layout view, when page loads

堅固的文本在我的ASP MVC 5應用程序中,我有一個(master- Jquery在這里加載_layout.cshtml和(子_layout.cshtml ,我的函數在這里可排序,而不是加載 )視圖tableView.cshtml

tableView.cshtml中,我編寫了需要在Jquery上調用的自定義JS函數。Read $ ready。 由於jquery已經加載到母版頁中,因此如何在子頁加載時附加我的函數(和第3部分插件)?

如果可能的話,請在子視圖加載/導航加載共享一種模塊化方式,以在子視圖中附加並初始化我的函數和第三方插件, 以便在加載主jquery函數時,它也調用我的函數。

_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

// 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!" );
    })();

您應該使用@RenderSection()作為占位符以從視圖中呈現內容

您的版面設計頁面可能看起來像

<!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>

並認為

@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>
}

注意@RenderSection("styles", false)位於<head>元素中,而@RenderSection("scripts", required: false)位於</body>標記的緊前面,這意味着視圖中定義的所有腳本都將在之后加載。頁面元素已加載(並在jquery文件之后)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM