简体   繁体   English

在IE11中使用指针事件时阻止单击?

[英]Prevent click when using pointer events in IE11?

I'm developing a JavaScript application that has to run on both IE11 and Edge. 我正在开发一个必须在IE11和Edge上运行的JavaScript应用程序。 In IE11, the event chain I'm seeing (copied from https://patrickhlauke.github.io/touch/tests/results/ ) is the following: 在IE11中,我看到的事件链(从https://patrickhlauke.github.io/touch/tests/results/复制)如下:

pointerover > mouseover > pointerenter > mouseenter > pointerdown > mousedown > (pointermove > mousemove)+ > pointerup > mouseup > (lostpointercapture) > pointerout > mouseout > pointerleave > mouseleave > focus > click 指针> mouseover> pointerenter> mouseenter> pointerdown> mousedown>(pointermove> mousemove)+> pointerup> mouseup>(lostpointercapture)> pointerout> mouseout> pointerleave> mouseleave> focus> click

The app is already wired up to handle mouse and touch events, so I call preventDefault() on all of the pointer events to cancel the corresponding mouse event. 该应用程序已经连线处理鼠标和触摸事件,所以我在所有指针事件上调用preventDefault()来取消相应的鼠标事件。 Unfortunately, the click comes through at the end and is causing problems. 不幸的是,点击在结束时出现并导致问题。 Is there any built in way to disable the click event from firing at the end? 有没有内置的方法来禁止点击事件在最后解雇?

I'm using jQuery Pointer Events Polyfill ( https://github.com/jquery/PEP ) to use the "pointerup" event on Webkit browsers and I ran into a similar problem. 我正在使用jQuery指针事件Polyfill( https://github.com/jquery/PEP )在Webkit浏览器上使用“pointerup”事件,我遇到了类似的问题。 In my case, I had to prevent a link from being followed. 就我而言,我不得不阻止链接被跟踪。

I resolved the problem by adding a "click" event listener right after "pointerup" was triggered, then preventing the "click" event and removing its listener. 我通过在“pointerup”触发后立即添加“click”事件侦听器来解决问题,然后阻止“click”事件并删除其侦听器。 Like this: 像这样:

var myLink = document.getElementsByClassName("myLink")[0];

myLink.addEventListener("pointerup", handleLinkPress);

function handleLinkPress(evt) {
    // do something here...

    evt.target.addEventListener("click", unfollowLink);

    function unfollowLink(evt) {
        evt.preventDefault();
        evt.target.removeEventListener("click", unfollowLink);
    }
}

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

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