Pre-binding and post-binding in js

Source: Internet
Author: User

The main meaning is that I have not used pre-binding, that is, when some elements in the Dom tree have not been created, it specifies some events that should be owned by elements of this type as soon as they are born. In actual development, pre-binding and post-binding are often involved. As the name suggests, some events are bound before the binding -- before being born, and only after the binding -- after birth.
The following is a simple example for your reference and comparison of each method:
Page elements:
<Div id = "main">
<A href = "#"> aaaaaaaaaaaaaaaaaaaaaa </a> <br/>
<A href = "#"> bbbbbbbbbbbbbbbbbbbbbb </a> <br/>
<A href = "#"> ccccccccccccccccccccccc </a> <br/>
<A href = "#"> dddddddddddddddddddd </a> <br/>
<A href = "#"> eeeeeeeeeeeeeeeeeeeeeeee </a> <br/>
<A href = "#"> fffffffffffffffffffffffffffffff </a> <br/>
<A href = "#"> gggggggggggggggggg </a> <br/>
<A href = "#"> hhhhhhhhhhhhhhhhhhhhhh </a>
</Div>
<Input type = "button" value = "Create a tag" id = "btnCreate"/>

Js in the page:
<Script src = "201102/Scripts/jquery-1.5.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// Post-binding, that is, the dynamically created element cannot own the Bound event !!!
// 1. Bind later
// $ ("# Main> a"). click (function (){
// Alert(%(this%.html ());
//});

// 2. Bind later
// $ ("# Main> a"). bind ("click", function (){
// Alert ($ (this). text ());
//});

// 3. Bind it later,
// $ ("# Main> a"). bind ({
// Click: function () {alert ($ (this). text ());},
// Mouseover: function () {callback (this).css ("background-color", "red ")},
// Mouseout: function () {callback (this).css ("background-color", "white ")}
//});
$ ("# BtnCreate"). bind ({
Click: function () {$ ("<br/> <a href = '#'> I created dynamically </a> "). appendTo ("# main ");}
});


// 4. Pre-bound, the dynamically created element also has a click event
// $ ("# Main"). delegate ("a", "click", function (){
// Alert ($ (this). text ());
//});

// 5. Pre-bind. The event source of live is the source of documentdelegate, which is the specific element to be bound. Therefore, the efficiency of delegate is much higher than that of live.
$ ("# Main a"). live ("click", function (){
Alert ($ (this). text ());
});
});
</Script>

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.