This article mainly introduces the difference between jquery's trigger and triggerHandler. Friends in need can refer to
trigger and triggerHandler both simulate event. Use a specific case to describe the difference
The code is as follows:
<html lang="en"> <head> <meta charset="utf-8"> <title>test</title> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <input type="checkbox" /> <input type="text" id="test"/> <input type="button" value="button" id="bnt" onclick ="bntClick()"/> </body> <script> $( document ).ready(function() { $("input[type='checkbox']").bind("click",function(){ $("#test").val("www.baidu.com"); }); }); function bntClick(){ $("input[type='checkbox']").trigger("click"); } </script> </html>
When the checkbox is clicked, the checkbox is checked and input[type='text'] is assigned the value www.baidu. com
When input[type='button'] is clicked, the same thing as clicking checkbox will happen.
When the trigger is replaced with triggerHandler, when input[type='button'] is clicked, only input[type='text'] is assigned a value without checking the checkbox
triggerHandler prevents the default behavior of the element bound to the event
The above is the detailed content of Example introduction to the difference between jquery's trigger and triggerHandler. For more information, please follow other related articles on the PHP Chinese website!