简体   繁体   English

<span><button>firefox 缺少内部元素的</button></span>点击事件

[英]Missing click event for <span> inside <button> element on firefox

I am experiencing problem with Firefox 32 when I bind action on click event of span element inside a button element.我在 Firefox 32 上遇到问题时,我在按钮元素内的 span 元素的单击事件上绑定操作。 Other browsers seems to works well.其他浏览器似乎运行良好。

Here the code illustrating the issue on jsFiddle .这里的代码说明了 jsFiddle 上的问题

 <span onClick="alert('test');">**Working**</span><br> <button>inside a button <span onClick="alert('test');">**Not working**</span></button>

Does anybody know why and if it's a bug or a feature?有谁知道为什么以及它是错误还是功能?

You have to add thepointer-events property to the span element.您必须将pointer-events属性添加到span元素。

button span {
  pointer-events: none;
}

As far as i known clicking the children elements won't work because it is nested inside a button.据我所知,单击子元素不起作用,因为它嵌套在按钮内。 A button is not container for child elements - only for text.按钮不是子元素的容器 - 仅用于文本。

If you try like below it always tell that you clicked the button.如果你像下面这样尝试,它总是告诉你你点击了按钮。

 <button type="button" onclick="alert('You clicked the button!');"> inside a button <span onClick="alert('clicked span');">**Not working**</span> </button>

So, my suggestion is to use another span or a div所以,我的建议是使用另一个跨度或一个 div

 <div class="button_replace"> inside a div <span onClick="alert('clicked span');">**working**</span> </div>

Hope it helps...希望能帮助到你...

Note: Your code will work in chrome but not in firefox and my answer is alternative solution for making work in firefox注意:您的代码可以在 chrome 中运行,但不能在 Firefox 中运行,我的答案是在 Firefox 中工作的替代解决方案

Add a pointer-event to your button in css like:在 css 中为您的按钮添加一个指针事件,例如:

 button span { pointer-events: none; }

Clicks on the button element will be ignored单击按钮元素将被忽略

Working在职的
inside a button Not working在按钮内不工作

What you actually want to do here is have a click event on the button - always the button, as that is the native HTML element and has built in accessibility for all.您真正想要在这里做的是在按钮上有一个点击事件 - 始终是按钮,因为它是原生 HTML 元素并且为所有人内置了可访问性。

The reason your button click won't fire if you click on something wrapped in a span (or or etc) inside a button is because you are listening for a click on event.target (what's actually clicked) and not on event.currentTarget.如果您单击按钮的跨度(或其他)包裹的内容,您的按钮单击不会触发的原因是因为您正在监听对 event.target(实际单击的内容)而不是 event.currentTarget 的单击。

If you want to listen to the button click itself (a click anywhere inside the button, including the span) you would use something like this:如果你想听按钮点击本身(点击按钮内的任何地方,包括跨度),你可以使用这样的东西:

HTML: HTML:

const myButton = document.querySelector('.jsAlertOnClick');

function handleBtnClick(event) {
  const btn = event.currentTarget;
  window.alert(btn + 'has been clicked!');
}

myButton.addEventListener('click', handleBtnClick);

Javascript: Javascript:

 const myButton = document.querySelector('.jsAlertOnClick'); function handleBtnClick(event) { const btn = event.currentTarget; window.alert(btn + 'has been clicked!'); } myButton.addEventListener('click', handleBtnClick);

If you click on the span, that alert will fire first, followed by the button one.如果您单击跨度,该警报将首先触发,然后是按钮一。 If you click elsewhere in the button, only that alert will fire.如果您单击按钮中的其他位置,则只会触发该警报。

Depending on the reason you have a span inside the button, you might be able to get rid of it entirely.根据按钮内有跨度的原因,您可以完全摆脱它。 I was just dealing with a similar issue where we had a span inside the button, but the span was being used to hold a glpyicon:我只是在处理一个类似的问题,我们在按钮内部有一个跨度,但这个跨度被用来保存一个 glpyicon:

    <button class="btn btn-default pull-right" type="button">
              <span class="glyphicon glyphicon-refresh"></span>
    </button>

I finally discovered there's an easier and cleaner way - at least in this case.我终于发现有一种更简单、更简洁的方法——至少在这种情况下是这样。 Just put the glyphicon class stuff inside the button class:只需将字形 class 放入按钮 class 中:

    <button class="btn btn-default pull-right glyphicon glyphicon-refresh" type="button">
    </button>

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

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