简体   繁体   English

在IE中点击按钮内未触发的事件

[英]click event on child inside button not firing in IE

I have a button with a span inside it. 我有一个带有跨度的按钮。 I have a click event on the button and I also have a click event on the span, but in IE11 the span click event is not firing (works in Chrome/Firefox). 我在按钮上有一个点击事件,我在跨度上也有一个点击事件,但在IE11中,跨度点击事件没有触发(在Chrome / Firefox中有效)。 Is there any workaround to this without changing from a button to a div? 如果没有从按钮更改为div,有没有解决方法?

I know that changing my button to a div will work (as answered in other questions), I want to avoid doing that. 我知道将我的按钮更改为div会起作用(如其他问题所述),我想避免这样做。

https://jsfiddle.net/asjo8ox0/2/ https://jsfiddle.net/asjo8ox0/2/

 $(document) .on("click", ".parent", function() { alert("parent"); }) .on("click", ".child", function(e) { alert("child"); e.stopPropagation(); }) 
 .parent { width: 200px; height: 200px; background-color: cornflowerblue; } .child { width: 100px; height: 100px; background-color: white; display: none; } .parent:hover .child { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="parent"> <span class="child"></span> </button> 

I know that is mentioned in the question that he wants to avoid using a div as a button. 我知道在问题中提到他想避免使用div作为按钮。 But it's not possible to achieve it in IE without doing some dirty code. 但是如果不做一些脏代码就不可能在IE实现它。 The best solution would be to change the button into a div . 最好的解决方案是将button更改为div

Here's the updated fiddle: https://jsfiddle.net/ovhhdab5/ 这是更新的小提琴: https//jsfiddle.net/ovhhdab5/

Had the same problem on one of my websites with IE and Buttons.. And i had a really long night because we found out directly after go-life.. :D When you disable the parent-click event you'll see that nothing happens and the child-click event is never fired when you click on the button. 在我的一个网站上使用IE和按钮也遇到了同样的问题..而且我度过了一个漫长的夜晚,因为我们在go-life之后直接发现..:D当你禁用父点击事件时你会看到没有任何反应单击按钮时,永远不会触发子单击事件。 It's a general problem with the IE. 这是IE的一般问题。

A possible solution: To avoid/solve the problem: Just add some lines of javascript/jquery to find out on what coordinates the button was clicked and when there was/is the child then fire the child-event instead. 一个可能的解决方案:避免/解决问题:只需添加一些javascript / jquery行就可以找出点击按钮的坐标,以及何时有孩子然后触发子事件。

You may be able to try something like this: 您可以尝试这样的事情:

parent.addEventListener("click", function(e) {
     var x = e.clientX, y = e.clientY,
     elementMouseIsOver = document.elementFromPoint(x, y);

     //Only run this on IE
     if (/MSIE|Trident/.test(window.navigator.userAgent);) {
         elementMouseIsOver.click()
     }
})

This will capture click events, and dispatch them the the correct child. 这将捕获点击事件,并将正确的子项发送给他们。

Be warned though: On IE only, this will lead to click events being dispatched to both the child and the parent. 但请注意:仅在IE上,这将导致将子事件分派给子级和父级。 While this was not a problem in my situation, it appears like it will be in yours, so you will need to find a way to stop the event from a. 虽然这在我的情况下不是问题,但看起来它会在你的身上,所以你需要找到一种方法来阻止事件发生。 bubbling and b. 冒泡和b。 being dispatched to other listeners on the parent. 被派遣到父母的其他听众。

https://jsfiddle.net/zLwagcmv/16/ https://jsfiddle.net/zLwagcmv/16/

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

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