按钮边缘在Chrome和Firefox中点击时出现问题。

huangapple go评论65阅读模式
英文:

Clicking edge of button is broken in Chrome & Firefox

问题

在Chrome和Firefox上,单击按钮的边缘会出现奇怪的行为:

CSS的活动状态被触发,但JavaScript的onclick事件没有被触发。只有在按钮的极边缘才会触发。

我尝试更改按钮的边框宽度(无边框和较粗的边框),但没有任何区别。

这在Chrome、Firefox和Opera中发生。但在Safari中没有。我没有测试Edge或IE。

以下是用于测试的确切代码:

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <button onclick="myFunction()">Click me</button>
  <p id="demo"></p>
</body>

</html>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}
button:active {
  transform: scale(.9);
}

按钮边缘在Chrome和Firefox中点击时出现问题。

英文:

On Chrome and Firefox, clicking the edge of a button gives odd behavior:

按钮边缘在Chrome和Firefox中点击时出现问题。

The CSS active state is triggered, but not the JavaScript onclick event. Only at the VERY EDGE of the button.

I tried changing the button border-width (no border and also a thick border). No difference.

It happens in Chrome, Firefox, & Opera. But not in Safari. I haven't tested Edge or IE.

Here is the exact code used to test this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {
  document.getElementById(&quot;demo&quot;).innerHTML = &quot;Hello World&quot;;
}

<!-- language: lang-css -->

button:active {
  transform: scale(.9);
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;button onclick=&quot;myFunction()&quot;&gt;Click me&lt;/button&gt;
  &lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

CSS状态在鼠标按下时更改。但是要注册点击事件,需要同时按下鼠标和释放鼠标按钮。如果我在按钮上按下鼠标,然后将鼠标移出按钮并释放,你会看到样式效果,但不会注册点击事件。

因为使用了scale(.9),所以在鼠标在按钮边缘附近按下时,鼠标指针在释放鼠标按钮时不再位于按钮上。

英文:

The CSS state is changed on mouse down. However to register the click, both mouse down and mouse up needs to be on the button. If I do a mouse down on the button, then move the mouse outside and let go, you see that style effect, but no click event is registered.

按钮边缘在Chrome和Firefox中点击时出现问题。

Because of the scale(.9), the mouse pointer is no longer over the button on mouse up when pressing near the edge.

huangapple
  • 本文由 发表于 2023年3月4日 07:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定