Overview
jQuery attaches an event handling function to all matching elements, even if the element is added later.
This method is basically a variant of the .bind() method. When using .bind(), elements matched by the selector will have an event handler attached to them, while elements added later will not. You need to use .bind() again for this. For example,
<body> <div class="clickme">Click here</div> </body>
can bind a simple click event to this element:
$('.clickme').bind('click', function() { alert("Bound handler called."); });
When the element is clicked, a warning box will pop up. Then, imagine that another element is added after this.
$('body').append('<div class="clickme">Another target</div>');
Although this new element can also match the selector ".clickme", since this element is added after calling .bind(), clicking on this element will have no effect.
.live() provides a method for this situation. If we bind the click event like this:
$('.clickme').live('click', function() { alert("Live handler called."); });
and then add a new element:
$('body').append('<div class="clickme">Another target</div>');
Then click on the new element, he can still trigger the event handler.
Event Delegation
The.live() method can be effective for an element that has not been added to the DOM due to the use of event delegation: the event handling function bound to the ancestor element can Respond to events triggered on descendants. The event handler passed to .live() will not be bound to the element, but will be treated as a special event handler and bound to the root node of the DOM tree. In our example, when a new element is clicked, the following steps will occur:
Generate a click event and pass it to
for processing
Since there is no event handling function Directly bound to Events continue to bubble up to the root node of the DOM tree. This special event handler is bound to it by default. Execute the special click event handling function bound by .live(). This event handling function first detects the target of the event object to determine whether it needs to continue. This test is implemented by checking whether $(event.target).closest('.clickme') can find a matching element. If a matching element is found, the original event handler is called. Since the test in step 5 above will only be done when the event occurs, elements added at any time can respond to this event. Additional Notes Although .live() is useful, due to its special implementation, it cannot simply replace .bind() in any case. The main differences are: In jQuery 1.4, the .live() method supports custom events and also supports all JavaScript events. In jQuery 1.4.1, there is even support for focus and blue events (mapped to the more appropriate, bubbling focusin and focusout). In addition, in jQuery 1.4.1, hover (mapped to "mouseenter mouseleave") is also supported. However, in jQuery 1.3.x, only supported JavaScript events and custom events are supported: click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup. .live() does not fully support elements found through DOM traversal. Instead, you should always use the .live() method directly after a selector, as mentioned in the previous example. When an event handling function is bound with .live(), to stop executing other event handling functions, this function must return false. Simply calling .stopPropagation() will not accomplish this. Refer to the .bind() method for more information about event binding. In jQuery 1.4.1, .live() can accept multiple events, separate events with spaces, and provide functions similar to .bind(). For example, we can "live" bind both the mouseover and mouseout events, like this: In jQuery 1.4.3: You can bind one or more event types to StringAnd function data mapping to execute them In jQuery 1.4, the data parameter can be used to pass additional information to the event handler function. A good use is to deal with problems caused by closures. See the discussion of .bind() for more information. In jQuery 1.4, the live event can be bound to the "context" DOM element instead of the default document root. To set this context, we use the jQuery() function's second argument on a single DOM element (rather than a jQuery collection or selector). 参数 type,[fn]String,FunctionV1.3 type:一个或多个事件类型,由空格分隔多个事件。 fn:要从每个匹配元素的事件中反绑定的事件处理函数 type,[data],falseString,Array,boolV1.4 type:一个或多个事件类型,由空格分隔多个事件。 data:传递给事件处理函数的附加参数 false:设置为false会使默认的动作失效。 eventStringV1.4.3 一个或多个事件类型的字符串和函数的数据映射来执行他们 示例 描述: 点击生成的p依然据有同样的功能。 HTML 代码: jQuery 代码: 描述: 阻止默认事件行为和事件冒泡,返回 false jQuery 代码: 描述: 仅仅阻止默认事件行为 jQuery 代码: 这是代码,用live绑定的事件不执行,但是创建的元素能添加进去。这是为什么 你的_html是字符串啊,你是不是要点击li触发事件? The above is the detailed content of Detailed explanation of the use of jQuery live event. For more information, please follow other related articles on the PHP Chinese website!$('.hoverme').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
// do something on mouseover
} else {
// do something on mouseout
}
});
$("a").live({
click: function() {
// do something on click
},
mouseover: function() {
// do something on mouseover
}
});
$('div.clickme', $('#container')[0]).live('click', function() {
// Live handler called.
});
<p>Click me!</p>
$("p").live("click", function(){ $(this).after("<p>Another paragraph!</p>"); });
$("a").live("click", function() { return false; });
$("a").live("click", function(event){ event.preventDefault(); });
那应该是$('li').live('click',function(){....})