简体   繁体   English

为什么浏览器在决定调用哪个 onclick 事件处理程序之前先应用 CSS styles?

[英]Why does a browser apply CSS styles first before deciding what onclick event handler to call?

I have a single div on a page.我在页面上有一个div It has an onclick event handler.它有一个onclick事件处理程序。 When the user clicks on that div I expect that the event handler will be called.当用户单击该div时,我希望将调用事件处理程序。 But there is this CSS rule as well: when that div has a focus, it will be moved 50 pixels to the right (away from the mouse pointer).但是也有这个 CSS 规则:当那个div有焦点时,它将向右移动 50 像素(远离鼠标指针)。 As a result of this the onclick event handler is not called, even though the user clearly clicked on that div at the beginning.因此, onclick事件处理程序未被调用,即使用户在开始时明确地单击了该div It's as if the browser first applies CSS and only after that decides, what did the user clicked on.就好像浏览器首先应用 CSS,然后才决定用户点击了什么。

I made a simple demo here: https://jsbin.com/yalazam/edit?html,css,console,output Click on the yellow square (that is the div ) in the fourth column and see the console in the third column.我这里做了一个简单的demo: https://jsbin.com/yalazam/edit?html,css,console,output点击第四栏的黄色方块(也就是div ),可以看到第三栏的控制台。 Only a message "focus square" will appear, but not a "click square" .只会出现消息"focus square" ,而不是"click square"

Does this behavior makes any sense?这种行为有意义吗? Is there any case when it is useful?有什么情况有用吗? Or should I just accept it as a weird behavior of the browser?或者我应该把它当作浏览器的一种奇怪行为来接受吗?

The relevant point here consists in the definition of what a click event is:这里的相关点在于什么是点击事件的定义

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.当指针位于元素内部时,当指针设备按钮(例如鼠标的主鼠标按钮)被按下和释放时,该元素会接收到click事件。 If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements.如果在一个元素上按下按钮,并且在释放按钮之前指针移到该元素之外,则会在包含这两个元素的最具体的祖先元素上触发该事件。

This means that in your example, no click event is fired on the div you're targeting, because although the mouse press happens inside that div, the release happens after it has moved (unless the user does something unnatural) so is not within the same element.这意味着在您的示例中,没有在您定位的 div 上触发click事件,因为尽管鼠标按下发生在该 div 内,但释放发生在它移动之后(除非用户做了一些不自然的事情)所以不在相同的元素。 A click event will fire on the body (which in your example is the "most specific ancestor" referred to on MDN), but that won't be terribly helpful to you to listen for - because a click on the body can happen in many other ways, and you can't event use the event's target property to see where it originated, because in this case that is the body itself as explained. click事件body上触发(在您的示例中,它是 MDN 上提到的“最具体的祖先”),但这对您的收听并不是很有帮助 - 因为在许多情况下都可能发生对bodyclick其他方式,并且您不能事件使用事件的target属性来查看它的起源,因为在这种情况下,正如所解释的那样,它就是body本身。

If you really need this effect then use the mouseDown or mouseUp event rather than click , depending on what feels most natural to you.如果您确实需要此效果,请使用mouseDownmouseUp事件而不是click ,具体取决于您感觉最自然的情况。 But the real answer is not to do this, as it is likely to be unexpected to your users and annoy them.但真正的答案是不要这样做,因为这可能会让您的用户出乎意料并惹恼他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 事件处理程序的正确名称是什么? onClick 还是 handleClick? - What is the right name of event handler? onClick or handleClick? DIV中的OnClick事件处理程序(First OnClick影响所有DIV) - OnClick event handler in DIVs (First OnClick affects all DIVs) 为什么只有 onclick 事件处理程序中的第一个 function 正确设置 innertext 而第二个给出 NaN? - Why only the first function inside onclick event handler sets innertext properly and the second one gives NaN? 为什么在onclick事件处理程序中使用return? - Why is return used in onclick event handler? SelectAll不调用事件处理程序 - SelectAll does not call Event Handler React组件不调用onClick处理程序 - React component does not call onClick handler 事件处理程序不适用于创建的元素 - Event handler does not apply to created element 如何在onClick事件之前调用动作方法 - How to call action method before onClick event 为什么当兄弟节点的“display”属性发生变化时,React组件的onClick事件处理程序不会触发? - Why does a React component's onClick event handler not fire when the “display” attribute of a sibling node is changed? 在反应中使用作用域 css 时,onClick 事件具有其所有 styles “” - onClick event have all their styles “” when using scoped css in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM