【问题标题】:Clicking a button's pseudo-element in Firefox does not trigger click在 Firefox 中单击按钮的伪元素不会触发单击
【发布时间】:2018-07-20 04:50:38
【问题描述】:

正如标题所示,在 Firefox 中单击按钮的伪元素不会触发我的点击。

此外,伪元素下方的文本和元素仍然是交互的。在这里看小提琴:

https://jsfiddle.net/15bmz7zg/1/

我使用的是 Firefox 58.0.1 - Mac。在 Chrome 和 Safari 上测试小提琴,一切都按预期工作。准备提交错误报告,但我想先看看其他人是否同意:这是一个错误,对吗?

2018 年 7 月 19 日更新 建议的副本不能解决这个问题。我已经提交了一份 Firefox 错误报告,并且该错误得到了确认。我将添加一个反映这一点的答案。

document.querySelector('button').addEventListener('click', function(){
	document.querySelector('button').classList.toggle('overlay')
})
button.overlay::after{
  content:"";
  position:fixed;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background:rgba(128,128,128,.5);
}
<h2>
I shouldn't be able to select this text when overlay is active.
</h2>
<p>
And, clicking the overlay should trigger button click.
</p>
<button>
toggle overlay
</button>

【问题讨论】:

  • 用“也”开始提问?除此之外还有什么?
  • 标题...我会复制/粘贴
  • 我使用的是最新版本的 Firefox 58.0.1(量子),您的小提琴按预期工作。更新后能再检查一下吗?
  • @Seth 我也在 Windows 10 上尝试了 FF 58.0.01,但它没有按预期工作
  • @Seth - 更新了问题以反映我也在使用 Firefox 58.0.1 - Mac OS。

标签: javascript html css


【解决方案1】:

这是 Firefox 中的一个已知错误,我的错误报告证实了这一点:https://bugzilla.mozilla.org/show_bug.cgi?id=1435331 ...被标记为此错误的副本:https://bugzilla.mozilla.org/show_bug.cgi?id=1089326

这个错误已经有 4 年的历史了,但显然现在正在处理中。

【讨论】:

    【解决方案2】:

    您可以将叠加层设置为非伪元素

    document.querySelector('button').addEventListener('click', function(){
    	document.querySelector('.overlay-wrap').classList.add('showed')
    });
    document.querySelector('.overlay-wrap').addEventListener('click', function(){
    	this.classList.remove('showed')
    });
    .overlay-wrap {
      display: none;
    }
    .overlay-wrap.showed{
      content:"";
      position:fixed;
      top:0;
      left:0;
      width:100%;
      height:100%;
      background:rgba(128,128,128,.5);
      display: block;
      z-index: 9999;
    }
    <h2>
    I shouldn't be able to select this text when overlay is active.
    </h2>
    <p>
    And, clicking the overlay should trigger button click.
    </p>
    <button>
    toggle overlay
    
    </button>
    <span class="overlay-wrap"></span>

    【讨论】:

    • 感谢您的解决方法!是的,有一些不同的选择,但如果我正确阅读规范,我的原始代码应该工作,并且原始代码对我来说更优雅/更灵活。如果它有效,我想使用它,并等待 Firefox 发布修复,如果它确实是一个错误。
    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2017-09-10
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2017-12-08
    相关资源
    最近更新 更多