Home  >  Article  >  Web Front-end  >  Example introduction to the difference between jquery's trigger and triggerHandler

Example introduction to the difference between jquery's trigger and triggerHandler

巴扎黑
巴扎黑Original
2017-06-25 13:59:171084browse

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=&#39;checkbox&#39;]").bind("click",function(){ 
$("#test").val("www.baidu.com"); 
}); 
}); 
function bntClick(){ 
$("input[type=&#39;checkbox&#39;]").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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn