$(function(){
$('#old').bind("click", function(){
$("input").trigger("focus");
});
$('#new').bind("click", function(){
$("input").triggerHandler("focus");
});
$("input").focus(function(){
$("body").append("<p>focus.</p>");
})
})
<button id="old">trigger</button>
<button id="new">triggerHandler</button>
<input />
$('#old').bind("click", function(){
$("input").trigger("focus");
});
$('#new').bind("click", function(){
$("input").triggerHandler("focus");
});
$("input").focus(function(){
$("body").append("<p>focus.</p>");
})
})
<button id="old">trigger</button>
<button id="new">triggerHandler</button>
<input />
本文通过两个按钮触发不同事件处理函数,对比展示了jQuery中trigger与triggerHandler的区别。点击按钮时,使用这两种方式来使输入框获得焦点,并附加了输入框获得焦点后的处理函数。
1942

被折叠的 条评论
为什么被折叠?



