fmpq.com
源代码:
点击运行
<!doctype html> <html> <head> <meta charset="utf-8"> <title>triggerHandler demo</title> <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script> </head> <body> <button id="old">.trigger( "focus" )</button> <button id="new">.triggerHandler( "focus" )</button><br><br> <p>执行 .trigger 后 input 输入框自动获取焦点,触发事件的默认行为,而 .triggerHandler 仅仅 执行了指定的事件浏览器并未执行动作,输入框也没有获取焦点。</p> <input type="text" value="将获取焦点"> <script> $( "#old" ).click(function() { $( "input" ).trigger( "focus" ); }); $( "#new" ).click(function() { $( "input" ).triggerHandler( "focus" ); }); $( "input" ).focus(function() { $( "<span>获取焦点!</span>" ).appendTo( "body" ).fadeOut( 1000 ); }); </script> </body> </html>
运行结果