Custom events for jquery are triggered by the on binding trigger

Source: Internet
Author: User

jquery binds custom events, enabling a pre-binding approach that uses jquery trigger to trigger custom events for quick and easy use when needed.
Let's assume a scenario like this, a textarea in the word count, if the direct keyboard input or paste in, it can be detected using the input method, but if it is inserted through JS text, this time the input event is not monitored,
This time if we bind a Mychange event beforehand, its callback function is to deal with the calculation of the number of words in the text, we use JS to this textarea assignment, concatenating write. Trigger ("Mychange") can be calculated.

Test code:

<textareaID= "textarea"></textarea><P><Buttontype= "button"ID= "BTN1">jquery Custom Event-Event Registration</Button></P><P><Buttontype= "button"ID= "BTN2">jquery Custom Event-Test trigger</Button></P><Pstyle= "margin-top:100px;"><ahref= "http://www.51xuediannao.com/">Lazy people build a station</a>http://www.51xuediannao.com/Finishing</P><Scriptsrc= "Http://libs.useso.com/js/jquery/1.11.1/jquery.min.js"></Script><Script>$btn 1= $("#btn1"); //jquery defines a custom event Diyevent Note: The first parameter in the callback function is an event, it needs to accept other parameters, it is not very good to follow the other parameters, and carefully control the "call example" to see$btn 1.on ("diyevent",function(Event,a,b,fun) {Console.log (A, b);    Fun ();     }); //jquery Custom Event Triggering example, note: the first parameter passed in trigger is a custom event name, the second parameter is an array, and the items in the array correspond to the parameters of the callback in the custom event    $("#btn2"). Click (function() {$btn 1.trigger ("diyevent",[" One"," A",function() {alert ("Lazy people build a station")}])    }) //Let's test the textarea scene.    varHaHa= function(){        varTest= function($el) {varLen=$el. Val (). length;        Console.log (len)}; $("#textarea"). On ("input PropertyChange",function() {Test ($ ( This)); }). On ("Mychange",function() {Test ($ ( This));    });    };     HaHa (); $btn 1.click (function(){        $("#textarea"). Val ("custom events for jquery are triggered by the on binding trigger"). Trigger ("Mychange")    })</Script>

This article links: jquery custom events trigger http://www.51xuediannao.com/js/jquery/832.html via on bind trigger

Custom events for jquery are triggered by the on binding trigger

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.