jQuery Live - 当项目添加到页面时触发

发布于 2024-12-09 23:03:29 字数 574 浏览 2 评论 0原文

我的 MVC 视图发生了 Ajax 更新。它会显示一条消息,告诉用户操作已完成:

<% if (ViewData["colorOptionsMessage"] != null) { %>
    <span class="ajaxMessage"><%= ViewData["colorOptionsMessage"] %></span>
<% } %>

我希望在该消息出现后自动淡出该消息,并且我希望执行一次,并使其在整个站点范围内工作。这是我尝试过的方法,但不起作用(出现消息,但不显示警报):

$(function () {
   $(".ajaxMessage").live("load", function () { 
      alert("once I can get this to show I'll put in a jueryUI fadeOut"); });
});

编辑

需要明确的是,我不需要淡出代码方面的帮助;我只需要帮助让 Live() 调用正确连接。

I've got an Ajax update happening to my MVC view. It displays a message telling the user the operation has completed:

<% if (ViewData["colorOptionsMessage"] != null) { %>
    <span class="ajaxMessage"><%= ViewData["colorOptionsMessage"] %></span>
<% } %>

I want to atuomatically fade this message out once it appears, and I'd like to do it once, and have it work site-wide. This is what I tried, which doesn't work (the message appears, but the alert does not show):

$(function () {
   $(".ajaxMessage").live("load", function () { 
      alert("once I can get this to show I'll put in a jueryUI fadeOut"); });
});

EDIT

Just to be clear, I don't need help with the fade out code; I just need help getting this call to Live() to wire up properly.

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

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

发布评论

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

评论(5

皇甫轩 2024-12-16 23:03:29

当新元素添加到页面时,没有直接的方法可以自动收到通知。 $('.ajaxMessage').live('load') 仅当使用 在图像、iframe 或对象/嵌入/小程序上触发 load 事件时才会发生>class="ajaxMessage",对于进入页面的每个新元素,不会使用其自己的目标触发 load 事件。

您只能通过 (a) 来执行此操作DOM 突变事件,通常没有得到足够广泛的支持,或者 (b) 不断轮询以获取 .ajaxMessage 选择器并查看结果中是否出现任何新元素。

最好在(可能)将内容添加到页面后立即在 ajax() 中手动 $('.ajaxMessage', function() {...})方法的success 处理程序。

预计到达时间:

jQuery 处理程序在 ajaxForm 成功后似乎没有执行

如果您无法直接捕获 success,您可以尝试使用 ajaxSuccess

There's no direct way to be informed automatically when new elements are added to the page. $('.ajaxMessage').live('load') would only happen when the load event fires on an image, iframe or object/embed/applet with class="ajaxMessage", a load event is not fired with its own target for every new element that enters the page.

You could only do this by (a) DOM Mutation Events, which generally aren't widely enough supported, or (b) constantly polling to fetch the .ajaxMessage selector and seeing if any new elements appear in the results.

Better to manually $('.ajaxMessage', function() {...}) immediately after (potentially) adding the content to the page, in the ajax() method's success handler.

ETA:

that jQuery handler doesn't seem to execute after an ajaxForm success

If you can't catch success directly you could try registering a global success handler using ajaxSuccess.

不如归去 2024-12-16 23:03:29

下面的代码期望您通过 jQuery 进行 Ajax 调用。如果 ajax 调用不是通过 jQuery,则忽略此答案。我们能看到 ajax 调用本身吗?

 <Script type="text/javascript">
    $(function () {
// this is a jQuery global ajax event that fires for every ajax, you need to check the URL
       $.ajaxSuccess(function(e, xhr, settings){
          if (settings.url == 'URI/ for/ajax /message/load.') {
          alert("once I can get this to show I'll put in a jueryUI fadeOut"); 
        }
       });
    );

    </script>

The code below expects you made the Ajax call through jQuery. if the ajax call was not through jQuery, then ignore this answer. Could we see the ajax call itself?

 <Script type="text/javascript">
    $(function () {
// this is a jQuery global ajax event that fires for every ajax, you need to check the URL
       $.ajaxSuccess(function(e, xhr, settings){
          if (settings.url == 'URI/ for/ajax /message/load.') {
          alert("once I can get this to show I'll put in a jueryUI fadeOut"); 
        }
       });
    );

    </script>
因为看清所以看轻 2024-12-16 23:03:29

当我想添加一些内容,等待一段时间,然后隐藏它时,我使用 setTimeout 函数而不是实时事件。我在成功回调中添加代码。

这是1秒的等待:

setTimeout(function(){ $("ajaxMessage").fadeOut(); }, 1000)

当然,如果你不想等待,只需在成功回调中淡出该元素即可。

这对你来说够了吗?

When i want to add something, wait a few, and hide it, I use setTimeout function instead of live events. I add the code on the success callback.

This is a 1 second wait:

setTimeout(function(){ $("ajaxMessage").fadeOut(); }, 1000)

Of course, if you dont want to wait, just fadeOut the element in the success callback.

Can this be enough for you?

忆离笙 2024-12-16 23:03:29

我不认为你可以将负载绑定到跨度。

仅适用于IMG IFRAME BODY

I dont think you can bind load to span.

only to IMG IFRAME BODY

神回复 2024-12-16 23:03:29

据我所知,jQuery 在“live”中不支持“ready”和“load”事件。因此,您可以使用插件 http://startbigthinksmall.wordpress.com /2011/04/20/announcing-jquery-live-ready-1-0-release/
这是演示 http://cdn.bitbucket.org/larscorneliussen/jquery .liveready/downloads/demo.html

As I know "ready" and "load" events are not supported by jQuery in "live". So you can use plugins as this one http://startbigthinksmall.wordpress.com/2011/04/20/announcing-jquery-live-ready-1-0-release/
Here is the demo http://cdn.bitbucket.org/larscorneliussen/jquery.liveready/downloads/demo.html

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