gpt4 book ai didi

jquery - .live() 可以用于 ajaxStart 和 ajaxComplete 吗?

转载 作者:行者123 更新时间:2023-12-01 00:20:30 25 4
gpt4 key购买 nike

我使用 jQuery 1.4.4,想知道是否可以将 .live() 用于 Ajax 事件,例如 ajaxStart/ajaxComplete。我已经用 .bind() 绑定(bind)了这些事件,并且它一直有效

jQuery(sourceId).bind("ajaxSend", function(event, xhr, ajaxOptions) {
// do something
});

equivalent

jQuery(sourceId).ajaxSend(function(event, xhr, ajaxOptions) {
// do something
});

与 .live() 的绑定(bind)更适合我的用例。有可能吗?我在某处读到以下代码片段不起作用

jQuery(sourceId).live("ajaxSend", function(event, xhr, ajaxOptions) {
// do something
});

预先感谢您的回复。

最佳答案

您不能使用 live() 来执行此操作,尽管您可以使用自定义事件来执行此操作。

live() 的含义是动态添加元素。动态加载的元素必须作为某些事件或 AJAX 回调的结果来完成,以便在回调事件上设置新的事件绑定(bind)。

callback event...
//code thatadds the new elements...

jQuery('selector that identifies the new elements').bind("ajaxSend", function(event, xhr, ajaxOptions) {
// do something
});

您可能希望将代码包装在函数中。

看看这里的评论,复制自http://api.jquery.com/live/

The .live() technique is useful, but due to its special approach cannot be simply substituted for .bind() in all cases. Specific differences include:

DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector, as in the example above.
To stop further handlers from executing after one bound using .live(), the handler must return false. Calling .stopPropagation() will not accomplish this.
In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live(): click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events that bubble.
As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout).
As of jQuery 1.4.1 the hover event can be specified (mapping to mouseenter and mouseleave, which, in turn, are mapped to mouseover and mouseout).

关于jquery - .live() 可以用于 ajaxStart 和 ajaxComplete 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5564779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com