在部分视图中定义 jquery 就绪事件

发布于 2024-11-28 06:06:18 字数 362 浏览 0 评论 0原文

我已经在 Site.Master 页面中定义了一个 $(document).ready() 事件,我还想在我的部分视图之一中定义另一个 $(document).ready() 事件(用于显示消息和错误消息) ,并且我在所有页面和所有部分视图中调用此部分视图...

部分视图显示在页面中并且还使用模式弹出窗口...所以我尝试这样做,但部分视图中的就绪事件没有触发

我有几件事要问:

  • 首先,是否可以做我做的事情我正在尝试做...
  • 有些页面具有部分视图,因此页面有两个 $(document).ready() 事件,因此当加载页面时,这两个事件之间是否存在任何冲突...

如果有人可以提供一些例子......

I have defined a $(document).ready() event in Site.Master page and I also want to define another $(document).ready() in one of my partial view (which is use to display msgs and error msgs), and I am calling this partial view in all pages and all partial view ...

the partial views are displayed in the page and also using modal popup ... so I tried to this but the ready event in partial view is not firing

I have few things to ask:

  • first, is it possible to do what i am trying to do ...
  • there are pages which have partial view and because of it a page has two $(document).ready() events so when the page is loaded, is there be any clashes between these two events ...

and if some body can provide wth some example ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-12-05 06:06:18

是的,您可以在页面上包含多个就绪事件处理程序。您可以将它们放入站点母版、部分视图和视图页面本身中——您需要的数量都可以。它们必须全部包含在脚本标签中。它们将按照它们包含在最终渲染页面中的顺序触发。请注意,您需要小心确保该部分仅在页面上包含一次,或者多次调用该处理程序也没关系。

示例(不完整):

主:

 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jqueryui.js"></script>
 <script type="text/javascript">
      $(function() {
           // do something for whole page
      });
 </script>

 @Html.Partial( "ErrorDialog" )

部分(ErrorDialog)

 <div id="errorDialog" style="display: none;" title="Error">
     <p>An error occurred</p>
 </div>
 <script type="text/javascript">
      $(function() {
          $('#errorDialog').dialog({
             modal: true,
             autoOpen: false,
             // more options
          });
      });

      function showError(msg) {
          $('#errorDialog').find('p').html(msg)
                           .stop()
                           .dialog('open');
      }
 </script>

Yes, you can include multiple ready event handlers on the page. You can put them in the site master, partial views, and view page itself -- as many as you need. They must all be enclosed in script tags. They will fire in the order that they are included in the final, rendered page. Note, you want to be careful to make sure that the partial is included only once on the page or that it doesn't matter if that handler is called multiple times.

Example (not complete):

Master:

 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jqueryui.js"></script>
 <script type="text/javascript">
      $(function() {
           // do something for whole page
      });
 </script>

 @Html.Partial( "ErrorDialog" )

Partial (ErrorDialog)

 <div id="errorDialog" style="display: none;" title="Error">
     <p>An error occurred</p>
 </div>
 <script type="text/javascript">
      $(function() {
          $('#errorDialog').dialog({
             modal: true,
             autoOpen: false,
             // more options
          });
      });

      function showError(msg) {
          $('#errorDialog').find('p').html(msg)
                           .stop()
                           .dialog('open');
      }
 </script>
谁对谁错谁最难过 2024-12-05 06:06:18

是的,您可以在页面中拥有多个 $(document).ready() ,只需确保在调用此函数之前已包含 jquery 文件即可。在 $(document).ready() 内调用的函数按照请求的顺序调用。

jQuery - 多个 $(document).ready ...?

yes you are allowed to have multiple $(document).ready() in a page just make sure you have included jquery file before calling this function. Functions invoked inside $(document).ready() called in the order they are requested.

jQuery - multiple $(document).ready ...?

烟雨凡馨 2024-12-05 06:06:18

是的,没错,只要之前的部分视图没有任何错误,您就可以拥有任意数量的 $(document).ready() ,并且它会为所有人触发。

Yep, that's right, as long as previous partial view does not have any error you can have as many $(document).ready() as you want and it will fire for all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文