jquery Method Binding (event registration) Code Summary

Source: Internet
Author: User
Tags define function

1. The most direct mode, directly passing a function object into the method function, such as the following click (), the advantages of the disadvantage of a glance to know

$ ("#btnComfirmChooseCompany"). Click (function () {    varCompanyName = ""; varCompanyID = ""; varhaschecked =false; varAllcheckbox = $ ("input[name= ' CompanyID ')");  for(vari = 0; i < allcheckbox.length; i++) {        if(allcheckbox[i].checked = =true) {haschecked=true; CompanyID= $ (Allcheckbox[i]). attr ("value")); CompanyName= $("#"+CompanyID). Parent (). Next (). text (); }    }    //slightly});

2. Equivalent notation, define function independently to a variable

function Comfirmchoosecompany (e) {  //e is optional parameter    // ibid., slightly }// or var function (){}

The variable is then passed into the JS event function, and the effect feels the same, but as to what the difference remains to be understood

$ ("#btnComfirmChooseCompany"). Click (    comfirmchoosecompany// can run, think ... What is the effect of adding parentheses to company? A: Comfirmchoosecompany run directly ); $ ("#btnComfirmChooseCompany"). Click (function() {         // equivalence notation, consider whether this business parenthesis can be omitted     });

3.bind method

$ ("#allCheck"). Bind function () {    $ ("[name = $chkItem]:checkbox"). attr ("Checked", $ (this). attr ("checked"));});

4.delegate method

$ ("Body"). Delegate function () {    $ (this). addclass (' Tr_color '). Siblings ("tr"). Removeclass ("Tr_color");}); 


jquery Method Binding (event registration) Code Summary

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.