jquery Dynamic binding elements

Source: Internet
Author: User

Changing the normal binding event to a static element to a dynamic element binding event will be used in the following way:The simple thing is to bind the event to the parent element of the binding element, which indicates which element under the parent element to bind to, so that regardless of whether your element is new, there are bound events, and the code is as follows:
<div class= "Btn-group listtype" >    <button id= "ListType" class= "btn" value= "style=" min-width:280px;_ width:280px; "data-toggle=" dropdown ">== Please select ==</button> <button    class=" Btn Dropdown-toggle " Data-toggle= "dropdown" ><span class= "caret" ></span></button>    <ul id= "Listtype_ul" class= "Dropdown-menu" style= "min-width:300px;_width:300px;" >        <li><a href= "javascript:void (0)" value= "1001" > Menu 1</a></li>        <li><a href= "javascript:void (0)" value= "1002" > Menu 2</a></li>    </ul></div>  

To bind the final bind event of the A element, but here a is loaded with a dynamic load, so we have to bind class= "Dropdown-menu" ul first, then the on event 2nd parameter indicates a element

$ (". ListType. Dropdown-menu"). On ("Click", "a",function() {    $ ("#listType"). Text ($ (This  ). Text ());    $ ("#listType"). attr ("Value", $ (this). attr ("value"));})

jquery Dynamic binding elements

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.