javascript Microsoft Edge:onclick 事件停止工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33525721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2021-01-28 16:35:35  来源:igfitidea点击:

Microsoft Edge: onclick event stops working?

javascriptjavascript-eventsonclickmouseeventmicrosoft-edge

提问by Corne

I have strange problems with my (ASP.NET) web application in Microsoft Edge.

我的 (ASP.NET) Web 应用程序在 Microsoft Edge 中遇到了奇怪的问题。

At a certain point the onclickevent stops working. All buttons on the page that respond to the onclickevent stop working. On the same page I have some buttons that respond to the onmousedownevent and they keep working.

在某个时刻,onclick事件停止工作。页面上响应onclick事件的所有按钮都停止工作。在同一页面上,我有一些响应onmousedown事件的按钮,它们继续工作。

If I refresh the page, the problem is gone. There are no errors in the console. I do not have this problem with other browsers (including IE11 under Windows 10).

如果我刷新页面,问题就消失了。控制台中没有错误。我在其他浏览器(包括 Windows 10 下的 IE11)上没有这个问题。

Did any of you experience similar problems?

你们中有人遇到过类似的问题吗?

回答by Will

This is not a complete answer, as it doesn't address whyclickevents don't work, but I feel it belongs here, as my team and I were hopelessly stuck trying to find an answer to this question. It appears to be a bug in Edge, and every workaround we tried failed, until I stumbled upon @Come's comment above.

这不是一个完整的答案,因为它没有解决为什么click事件不起作用,但我觉得它属于这里,因为我的团队和我无可救药地试图找到这个问题的答案。这似乎是 Edge 中的一个错误,我们尝试的所有解决方法都失败了,直到我偶然发现了上面@Come 的评论。

The solution was to change all of our clickevents to mouseupevents, emulating the same behavior. For some reason mouseupwas triggered even when clickwasn't. mousedownworks as well, but mouseupmore-properly emulates click.

解决方案是将我们所有的click事件更改为mouseup事件,模拟相同的行为。出于某种原因mouseup,即使click没有被触发。mousedown也可以工作,但mouseup更适合模拟click.

var listenType = (navigator.userAgent.toLowerCase().indexOf('edge') != -1) ? 'mouseup' : 'click';

// ...

$("#my_element").on(listenType, function() 
{
    // ...
}

Hopefully this helps someone!

希望这对某人有所帮助!

回答by Kelly Elton

This is a bug in Edge.

这是Edge中的一个错误。

What I've found is that it's related to both popping up a child window and rearranging(or modifying) tr's in a table.

我发现它与弹出子窗口和重新排列(或修改)表格中的 tr 相关。

https://connect.microsoft.com/IE/feedback/details/2109810/browser-stops-registering-click-events-on-table

https://connect.microsoft.com/IE/feedback/details/2109810/browser-stops-registering-click-events-on-table

To fix this, you have to force the table to redraw. You can do this by setting display:none before modifying the table, and then setting display:previousValue after changing it.

要解决此问题,您必须强制重新绘制表格。您可以通过在修改表之前设置 display:none ,然后在更改后设置 display:previousValue 来做到这一点。

Hope this helps.

希望这可以帮助。

回答by neptunian

My fix was to create a "hidden" input element and call focus on it which magically makes the clicks return.

我的解决方法是创建一个“隐藏的”输入元素并调用它的焦点,这神奇地使点击返回。

回答by KNaito

In my case, removing child elements bring this problem. Therefore when I remove the element (including replace DOM by innerHTML), I make style.display = "none"for the element before removing. This resolves the issue.

就我而言,删除子元素会带来这个问题。因此,当我删除元素(包括用 innerHTML 替换 DOM)时,我会style.display = "none"在删除之前制作该元素。这解决了这个问题。

For example, I use this function

例如,我使用这个功能

function removeComponent(selector) {
  $(selector).css("display", "none");
  return $(selector).detach();
}

instead of

代替

function removeComponent(selector) {
  return $(selector).detach();
}