Has been plagued by this problem before, after the jQuery1.7 version of the Add on method, previously learned, its superiority than live (), bind (), delegate () and other methods, before this project to use this to test the results found, Incredibly dynamically generated tags clicked did not respond, and live method can support, and so everywhere search information, ask netizens, results for a long time in an article finally found the answer ...
When JQuery uses on to bind dynamically generated elements, it cannot manipulate the object directly, but instead chooses its non-dynamically generated parent node and then finds itself to achieve the effect. Everybody look at the source code to know. The generated button cardinality entry on method is valid by clicking the invalid Live method. UI front-end frame latest masterpiece! Prize Probation!
For example, there are two elements below the page:
. Code
- <input type= "button" Name= "addbtn" value= "button Add"/>
- <div id= "Test" ></div>
Using the jquery code below you can compare the differences:
. Code
- $ (function () {
- var a = 1,
- $_div = $ (' #test ');
- $ (' input[name=addbtn] '). On (' click ', function () {
- $_div.append (' <input type= ' button "name=" Test ' + A + ' "value=" buttons ' + A + ' "/> ');
- a++;
- });
- Even item click events
- $_div.on (' Click ', ' Input[name^=test]:even ', function () {
- Alert (' I am a valid on method, can you see me: ' + this.value);
- });
- An odd-bound Click event finds the click is invalid, but instead uses the live method to support
- $ (' input[name^=test]:odd '). On (' click ', function () {
- Alert (' I am invalid on method, you cannot see me ');
- });
- An odd-bound Click event finds the click is invalid, but instead uses the live method to support
- $ (' input[name^=test]:odd '). Live (' click ', function () {
- Alert (' I am the live method, can you see me: ' + this.value);
- });
- });
The code is simple, do not put the demo page, if there is nothing to understand, you can leave a message. UI front-end frame latest masterpiece! Prize Probation!
JQuery on () method binds a dynamic element's Click event