[英]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 上提到的“最具体的祖先”),但这对您的收听并不是很有帮助 - 因为在许多情况下都可能发生对body
的click
其他方式,并且您不能事件使用事件的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.如果您确实需要此效果,请使用mouseDown
或mouseUp
事件而不是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.