Document directory
Returned value: jquerylive (type,
[Data], FN) Overview
Jquery attaches an event handler to all matching elements, even if the element isIt will also be valid if you add it later.
This method is basically a variant of the. BIND () method. When. BIND () is used, an event processing function will be appended to the element matched by the selector, and no elements will be added later. Therefore, you need to use. BIND () again. For example
<body> <div class="clickme">Click here</div></body>
You can bind a simple click event to this element:
$('.clickme').bind('click', function() { alert("Bound handler called.");});
When an element is clicked, a warning box is displayed. Then, imagine that another element was added.
$('body').append('<div class="clickme">Another target</div>');
Although this new element can also match the selector ". clickme", clicking this element will not work because it is added after. BIND () is called.
. Live () provides a method for this situation. If we bind a click event like this:
$('.clickme').live('click', function() { alert("Live handler called."); });
Then add a new element:
$('body').append('<div class="clickme">Another target</div>');
Then, click the new element to trigger the event processing function.
Event Delegate
. The live () method is effective for an element that has not been added to the Dom because event delegation is used: event Handlers bound to the ancestor element can respond to events triggered on future generations. The event handler passed to. Live () is not bound to an element. Instead, it serves as a special event handler and binds it to the root node of the DOM tree. In our example, when a new element is clicked, the following steps are taken in sequence:
- Generate a click event and pass it to <div> for processing
- Since no event handler function is directly bound to <div>, the event bubbles to the DOM tree.
- Events are continuously bubbling until the root node of the DOM tree. By default, this special event handler function is bound to it.
- Execute the special click event processing function bound by. Live.
- The event handler first checks the target of the event object to determine whether to continue. This test is performed by checking whether matching elements can be found in condition (event.tar get). Closest ('. clickme.
- If matched elements are found, the original event handler is called.
Because the test is performed in step 5 of the preceding step only when an event occurs, the element added at any time can respond to the event.
Additional instructions
. Live () is useful, but it cannot simply replace. BIND () in any situation due to its special implementation method (). The main differences are:
- In jquery 1.4, the. Live () method supports custom events and all JavaScript events. In jquery 1.4.1, the focus and blue events are even supported (ing to focusin and focusout that are more appropriate and can be bubbling ). In addition, hover (mapped to "mouseenter mouseleave") is also supported in jquery 1.4.1 "). However, in jquery 1.3.x, only 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, the. Live () method should always be used directly after a selector, as mentioned in the previous example.
- When an event processing function is bound with. Live (), to stop executing other event processing functions, this function must return false. Only. stoppropagation () can be called.
Refer to the. BIND () method for more information about event binding.
In jquery 1.4.1, You can bind multiple events to. Live () at a time, similar to. BIND.
In jquery 1.4, the data parameter can be used to pass additional information to the event handler. A good use is to cope with problems caused by closures. For more information, see. BIND.
Http://my.oschina.net/u/135304/blog/31612