且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单击div时防止触发焦点事件

更新时间:2022-02-26 06:52:50

DEMO 这里

DEMO here

我想你在这里遗漏了一些零件,

I think you are missing some parts here,


  1. event.stopPropagation()用于阻止事件冒泡。您可以在此处阅读相关内容。

  1. event.stopPropagation() is used to stop the event from bubbling. You can read about it here.

event.stopImmediatePropagation()除了保持元素上的任何其他处理程序不被执行外,此方法还通过隐式调用 event.stopPropagation来停止冒泡。 ()。你可以阅读这里

event.stopImmediatePropagation() In addition to keeping any additional handlers on an element from being executed, this method also stops the bubbling by implicitly calling event.stopPropagation(). You can read about it here

如果如果要停止浏览器事件,则应使用 event.preventDefault()。你可以在这里了解它

If you want to stop browser events, then you should use event.preventDefault(). You can read about it here

click = mousedown + mouseup - >当鼠标按下成功时,焦点将设置在HTML元素上。因此,不应绑定单击事件,而应使用'mousedown'。请参阅我的演示。

click = mousedown + mouseup -> The focus will be set on a HTML element when the mouse down is successful. So instead of binding click event, you should use 'mousedown'. See my demo.

您无法使用1个动作布尔值来确定单击了哪个div以及单击了多少次。检查我的演示,看看你如何处理它。

You cannot use 1 action boolean value to determine which div is clicked and how many times it has been clicked. Check my Demo to see how you can handle that.