简体   繁体   English

JavaScript 事件传播不适用于 Firefox 上的 HTML 按钮子元素。

[英]JavaScript Event propagation not working on Firefox on HTML Button child elements.

I am trying to pass JavaScript Function on HTML Button element and its Child elements, But child element function is not trigger on the Firefox.我试图在 HTML Button 元素及其子元素上传递 JavaScript 函数,但子元素函数不会在 Firefox 上触发。

Its working well on the Chrome and Safari.它在 Chrome 和 Safari 上运行良好。

Test here.在这里测试。

HTML Block: HTML 块:

<div class="container p-5">
  <button onClick="sayButton();" class="btn btn-block btn-primary">
    <span class="badge badge-light" onClick="sayDiv(event)">Hello</span>
  </button>
</div>

JavaScript function: JavaScript 函数:

function sayButton() {
 alert("button");
}

function sayDiv(e) {
 e.stopPropagation();
 alert("div");
}

Say Div function is not working on the firefox.说 Div 功能在 firefox 上不起作用。

Jsfiddle: https://jsfiddle.net/jainvabhi/nmp2L7ma/3/ jsfiddle: https ://jsfiddle.net/jainvabhi/nmp2L7ma/3/

Code Pen: https://codepen.io/jainvabhi/pen/VQNgZq代码笔: https : //codepen.io/jainvabhi/pen/VQNgZq

I am not sure if Firefox will let you do this with a button element as the parent.我不确定 Firefox 是否会让您使用按钮元素作为父元素来执行此操作。 In my understanding, according to the Content Model for the button, you should not have interactive children.根据我的理解,根据按钮的内容模型,您不应该有交互式子项。 That being said, if you change the button to a div like following, it will work.话虽如此,如果您将按钮更改为如下所示的 div,它将起作用。

<div class="container p-5">
  <div onClick="sayButton();" class="btn btn-block btn-primary">
    <span class="badge badge-light" onClick="sayDiv(event)">Hello</span>
  </div>
</div>

Fiddle Fork小提琴叉

If this is for a production application, please take into consideration potent accessibility considerations when using an element other than a button for this purpose.如果这是用于生产应用程序,请在为此目的使用按钮以外的元素时考虑有效的可访问性

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM