jquery触发事件的方法和trigger triggerHander的区别

<div>
<p>fff</p>
</div>
<button type="">d</button>

<input></input>

$("div").on(‘click‘, function(event) {
alert(22);
});

可以直接跟上方法名触发

 

$("button").click(function(event) {
$("div").click();
});

也可以用trigger

$("button").click(function(event) {
$("div").trigger("click");
});

$("button").click(function(event) {
$("input").trigger("focus");  // 获取input焦点
});

$("button").click(function(event) {
$("input"). triggerHander("focus");  // 不能获取input焦点
})

使用 triggerHander不能获取input焦点 因为triggerHander不能触发浏览器默认事件

相关文章