jQuery Live - 当项目添加到页面时触发
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当新元素添加到页面时,没有直接的方法可以自动收到通知。
$('.ajaxMessage').live('load')
仅当使用在图像、iframe 或对象/嵌入/小程序上触发
,对于进入页面的每个新元素,不会使用其自己的目标触发load
事件时才会发生>class="ajaxMessage"load
事件。您只能通过 (a) 来执行此操作DOM 突变事件,通常没有得到足够广泛的支持,或者 (b) 不断轮询以获取
.ajaxMessage
选择器并查看结果中是否出现任何新元素。最好在(可能)将内容添加到页面后立即在
ajax()
中手动$('.ajaxMessage', function() {...})
方法的success
处理程序。预计到达时间:
如果您无法直接捕获
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 theload
event fires on an image, iframe or object/embed/applet withclass="ajaxMessage"
, aload
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 theajax()
method'ssuccess
handler.ETA:
If you can't catch
success
directly you could try registering a global success handler usingajaxSuccess
.下面的代码期望您通过 jQuery 进行 Ajax 调用。如果 ajax 调用不是通过 jQuery,则忽略此答案。我们能看到 ajax 调用本身吗?
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?
当我想添加一些内容,等待一段时间,然后隐藏它时,我使用 setTimeout 函数而不是实时事件。我在成功回调中添加代码。
这是1秒的等待:
当然,如果你不想等待,只需在成功回调中淡出该元素即可。
这对你来说够了吗?
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:
Of course, if you dont want to wait, just fadeOut the element in the success callback.
Can this be enough for you?
我不认为你可以将负载绑定到跨度。
仅适用于IMG IFRAME BODY
I dont think you can bind load to span.
only to IMG IFRAME BODY
据我所知,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