This article is mainly on the jquery Live () repeated binding solution for the detailed analysis of the introduction, the need for friends can come to the reference, I hope to be helpful to everyone
How to use the Live () method Today encountered a problem writing code, directly on the code look: $ (function () { $ (". File "). Live (" Click ", Function () { var task_name=$ (this ). text (); $ ("#selecting tbody"). Append ("<trclass=gradea" &G t;<tdclass= ' center ' > ' +task_name+ ' </td></tr> '); }; }); $ (". File") objects are passed in from the background, the click is definitely not, bind () also cannot get dynamically added elements, so only live (), but the problem with live () is that the table line inexplicably added two lines, That is, the binding event is repeated, tangled up a morning finally found the reason, first look at Live () method introduction. Live (type, [DATA],FN) Overview jquery Appends an event handler to all matching elements, even if the element is added later. This method is basically yes. A variant of the bind () method. When you use. bind (), the element that the selector matches attaches an event handler function, and the elements that are added later are not. This needs to be reused once. bind () is OK. For example <body> <div class= "ClickMe" >Clickhere</div> <body> can bind a simple click event to this element: & nbsp $('.ClickMe '). Bind (' click ', Function () { alert ("Bound handler called."); &NBSP ; }); When you click on the element, a warning box pops up. Then, imagine that another element has been added after this. $ (' body '). Append (' <div class= "ClickMe" >another target</div> "); Although this new element can also match the selector ". ClickMe", because this element is added after calling. Bind (), clicking on this element will have no effect. Live () provides a way to respond to this situation. If this is how we bind the Click event: $ ('. ClickMe '). Live (' click ', function () {alert ("live handler called.") & nbsp }); then add a new element: $ (' body '). Append (' <divclass= "ClickMe" >Anothertarget</div> "); Then click on the new element, and he can still trigger the event handler function. Event delegates the Live () method is valid for an element that has not yet been added to the DOM because of the use of event delegates: event handlers bound on ancestor elements can respond to events triggered on descendants. The event handler function that passes to. Live () is not bound to the element, but rather as a special event handler, bound to the root node of the DOM tree. In our example, when the new element is clicked, the following steps occur: 1, generate a click event to be passed to <div> to process 2, because there is no event handler function directly bound to <div>, So the event bubbles up to the DOM tree 3, the event bubbles up to the root node of the DOM tree, and by default this particular event handler is bound. 4, performing a special C bound by. Live ()Lick event handler function. 5, this event handler first detects the target of the event object to determine if it needs to continue. This test is achieved by detecting whether the $ (event.target). Closest ('. ClickMe ') can find a matching element. 6, if a matching element is found, the original event-handler function is invoked. Because the test is done in the fifth step above only when the event occurs, the elements added at any time can respond to this event. Additional notes Live () is useful, but because of its special implementation, it cannot be easily replaced in any case. bind (). The major differences are: in jquery 1.4, the. Live () method supports custom events and all JavaScript events. In the jquery 1.4.1, the focus and blue events are even supported (mapped to more appropriate and bubbling focusin and focusout). In addition, hover (mapped to "MouseEnter MouseLeave") can also be supported in jQuery1.4.1. In jquery1.3.x, however, 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 the DOM traversal method. Instead, it should always be used directly behind a selector. The live () method, as mentioned in the previous example. When an event handler is bound with. Live (), the function must return FALSE if you want to stop performing other event-handling functions. Only the call to. Stoppropagation () does not accomplish this. Reference. The bind () method gets more information about event bindings. in jquery 1.4.1, you can bind multiple events to. Live (), 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. You can refer to the discussion of. Bind () for more information. &NBSP Parameters typestring Event type data (optional) object to bind event handlers & nbsp fn function binding event handling function &NB Sp Example HTML code: <p>Clickme!</p> JQuery code: $ ("P"). Live ("Click", Function () {$ (this). After ("& Lt;p>anotherparagraph!</p> "); }); Description: Prevent default event behavior and event bubbling, return false JQuery code: $ ("a"). Live ("Click", Function () {return false;}); //root cause here, you need to block default event behavior and event bubbling, add return false after code; OK Description: Just block default event behavior JQuery code: $ ("a"). Live (" Click ", Function (event) {Event.preventdefault ();});