Javascript blur与click冲突解决办法(2)

<input type="button" value="点我"> var btn = document.getElementById("btn"); btn.addEventListener("mousedown", function(){ console.log("my button mousedown"); }); btn.addEventListener("click", function(){ console.log("my button click"); }); btn.addEventListener("mouseup", function(){ console.log("my button mouseup"); }); 输出结果: my button mousedown my button mouseup my button click

所以,其执行顺序为:

mousedown >> mouseup >> click

解决方案2:将click事件改为mousedown,让其优先于blur事件执行

(function($){ $("input").focus(function(){ // input框获取焦点,展示下拉框 $(".search-list").attr("data-status", "show"); }).blur(function(){ // input框失去焦点,隐藏下拉框 $(".search-list").attr("data-status", "hide"); }); // 选择对应选项,并赋值给input框 $(".search-list li a").mousedown(function(){ $("input").val($(this).text()); }); })(jQuery);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjyws.html