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