jquery tagging does not respond to click events

Source: Internet
Author: User
jquery tagging does not respond to click eventsOne problem with the project today is to add <li></li> tags dynamically using jquery, and then add the click event to Li. The starting code is as follows
<div class= "Main-content" >
    <button class= "btn btn-positive btn-block loadbut" ><strong> loading ... </strong></button>
</div>
After adding the UL label
<div class= "Main-content" >
    <button class= "btn btn-positive btn-block loadbut" ><strong> loading ... </strong></button>
    <ul class= "Table-view connect-list" >
        <li class= "Table-view-cell Odd-list ">item 1</li> <li class=" Table-view-cell even-list ">item 2</li> <li-class=
        " Table-view-cell odd-list ">item 1</li>
        <li class=" Table-view-cell even-list ">Item 2</li>
        <li class= "Table-view-cell odd-list" >item 1</li> <li class=
        "Table-view-cell even-list" > Item 2</li>
    </ul>

Then add the Click event to the Li label to not respond.
Since the beginning of loading documents, the LI tag does not exist at all, how can respond to the event. So in response to this situation. The best way to do this is to have the parent element find the subclass element to see if he fits the element. How to do it, in JS, most of the events require 3 stages of capture-processing-bubbling. Except for a few events such as Blur. Most can be found to match the elements to be processed through the bubbling process of the parent class-subclass. Like the example above. In the event of the Ul bubbling process, will pass the element of Li, we will be in the process of processing our business. jquery provides us with a function such as on () to view the document ourselves. $ ('. Main-content '). On (' Click ', ' Li ', function (e) {
alert (this);
});

ps:$ (parent class selector). On ("event", "subclass selector", handler function);
This on () usage is not the same as live usage, which is distinguished.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.