Query中.live()方法的使用方法
今天在写代码的时候遇到一个问题,直接上代码看:
$(function(){
$(".file").live("click",function(){
var task_name=$(this).text();
$("#selecting tbody").append("
});
});
$(".file")对象是从后台传过来的,click肯定是不行的,bind()也无法获取动态添加的元素,因此只能用live(),但是使用live()遇到的问题是表格行会莫名其妙的添加了两行,即绑定事件重复执行了,纠结了一上午终于找到了原因,先看live()方法的介绍。
live(type, [data],fn)
概述
jQuery给所有匹配的元素附加一个事件处理函数,即使这个元素是以后再添加进来的也有效。
这个方法是基本是的 .bind() 方法的一个变体。使用 .bind()时,选择器匹配的元素会附加一个事件处理函数,而以后再添加的元素则不会有。为此需要再使用一次 .bind() 才行。比如说
可以给这个元素绑定一个简单的click事件:
$('.clickme').bind('click', function() {
alert("Bound handler called.");
});
当点击了元素,就会弹出一个警告框。
然后,想象一下这之后有另一个元素添加进来了。
$('body').append('
尽管这个新的元素也能够匹配选择器".clickme" ,但是由于这个元素是在调用 .bind() 之后添加的,所以点击这个元素不会有任何效果。
.live()就提供了对应这种情况的方法。如果我们是这样绑定click事件的:
$('.clickme').live('click', function() {
alert("Live handler called.");
});
然后再添加一个新元素:
$('body').append('
Then click on the newly added element, he can still trigger the event handler function.
Event Delegate
The.live() method works on an element that has not yet been added to the DOM due to the use of event delegation: event handlers bound to ancestor elements 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 occur:
1. Generate a click event and pass it to 2. Since there is no event handling function directly bound to 3. Events continue to bubble up to the root node of the DOM tree. This special event handler is bound to it by default. 4. Execute the special click event processing function bound by .live(). 5. This event processing 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. 6. If a matching element is found, call the original event handling function. Since the test in step 5 above is only done when the event occurs, elements added at any time can respond to this event. .live() is useful, but due to its special implementation, it cannot simply replace .bind() in any situation. The main differences are: In jQuery 1.4, the .live() method supports custom events and all JavaScript events. In jQuery 1.4.1, focus and blue events are even supported (mapped to the more appropriate and bubbling focusin and focusout). In addition, in jQuery1.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 processing function is bound with .live(), and you want to stop executing other event processing functions, this function must return false. Simply calling .stopPropagation() cannot achieve this purpose. In jQuery 1.4.1, you can bind multiple events to .live() at one time, similar to the functionality provided by .bind(). 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. typeString Event type data (optional) Object The event handler function to be bound fn Function The event processing function to be bound HTML code: Clickme! jQuery code: Anotherparagraph!< ;/p>"); Prevent default event behavior and event bubbling, return false jQuery code: //The root cause is here, you need to prevent the default event behavior and event bubbling, add return false; after the code, it will be OK Description: Only prevent default event behavior jQuery code:
Additional Notes
Refer to the .bind() method for more information about event binding.
Parameters
Example
$("p").live("click", function(){
$(this).after("
});
Description:
$("a").live("click",function() { return false; });
$("a").live("click", function(event){
event.preventDefault();
});