[英]How to preventDefault for event A from event B handler
I have an event handler attached to touchstart and I want to call preventDefault
as soon as touchmove occurs. 我在touchstart上附加了一个事件处理程序,我想在touchmove发生时立即调用
preventDefault
。 I have this code currently. 我目前有此代码。
link.addEventListener("click", function () {
console.log("clicked");
});
link.addEventListener("touchstart", function (touchStartEvent) {
var mouseMoveHandler = function () {
console.log("moved.");
touchStartEvent.preventDefault(); // This does not work.
link.removeEventListener('touchmove', arguments.callee);
};
link.addEventListener("touchmove", mouseMoveHandler);
});
http://jsfiddle.net/682VP/ http://jsfiddle.net/682VP/
I'm calling preventDefault
for touchstart within an event handler for touchmove. 我正在为触摸移动事件处理程序中的touchstart调用
preventDefault
。 This does not seem to work because the click event handler is always invoked. 这似乎不起作用,因为始终会调用click事件处理程序。
What am I doing wrong here? 我在这里做错了什么?
When you call preventDefault
it works for touchstart event, not for click event. 当您调用
preventDefault
它适用于touchstart事件,而不适用于click事件。
You can add some property for the link object indicating the moving state and check it inside click handler itself (and stop it either by preventDefault
or return false
) 您可以为链接对象添加一些指示移动状态的属性,并在点击处理程序本身中检查它(并通过
preventDefault
或return false
停止它)
var link = document.getElementById("link");
link.addEventListener("click", function () {
if (this.moving) {
this.moving = false;
return false;
}
console.log("clicked");
});
link.addEventListener("touchstart", function (touchStartEvent) {
var mouseMoveHandler = function () {
console.log("moved.");
this.moving = true;
link.removeEventListener('touchmove', arguments.callee);
};
link.addEventListener("touchmove", mouseMoveHandler);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.