Namespace usage for the Bind event in jquery (GO)

Source: Internet
Author: User

  • Scenario : An element on the page bind multiple click event handlers, depending on the user's specific interaction, to determine which handler to use.
  • Questions :
    1. Unbind will unbind all click events, causing accidental injury. If the previous bind has a method name that defines the handler, you can unbind the specified bind by passing the method name to Unbind's second argument, but in many cases the handler function of BIND is an anonymous function, at which point the second parameter of unbind cannot be processed.
    2. When the Click event occurs, you cannot specify that a specific event handler is called, only the function that should not be called is unbind out.
  • resolution : Using namespaces, Namespace usage
  • <!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <title></title>    <Scripttype= "Text/javascript"src= "Script/jquery-1.7.1.min.js"></Script>    <Scripttype= "Text/javascript">        functionMyFunction1 () {alert (1); return false; }        functionMyfunction2 () {alert (2); return false; }        functionMyfunction3 () {alert (3); return false; }        $(function () {            $('#test'). Bind ('click.a', MyFunction1); $('#test'). Bind ('click.b', Myfunction2); $('#test'). Bind ('click.c', Myfunction3); $('#test'). Unbind ('click.a'); //$ (' #test '). Trigger (' click.b ');            $('#test'). Trigger (); //$ (' #test '). Click ();        })    </Script></Head><Body>    <inputID= "Test"type= "button"name= "button"value= "button"width= "The "></Body></HTML>

    Add '. ' After the event name (click) To specify the new naming, in this case, the new named A, B, and C, respectively, so that the specified function can be unbound at unbind, and the specified function can be called at trigger time. Many JS extensions use namespaces to differentiate themselves from events and avoid conflicts with the original events on the page.

  • Reference Link: http://blog.csdn.net/clangke/article/details/7612224

Namespace usage for the Bind event in jquery (GO)

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.