【问题标题】:How would I cancel a specific event in javascript?如何取消javascript中的特定事件?
【发布时间】:2019-04-07 05:05:43
【问题描述】:

我在 on 方法中添加了 2 个不同的事件,每个事件分别称为 touchstartmousedown,如下所示:

this.$el.on('touchstart mousedown', (e) => { this.start(e); })

我的主要目标是在调用 end 函数的末尾取消属于 on 方法的 mousedown 事件

如果我运行 touchstart 事件 2 个不同的事件总是一起触发。看这张图:

此问题会导致 Firefox 出现意外事件阻塞。而且代码运行两次,这是不必要的。我想在我clicktouch 元素时重定向网站,当我dragswipe 时阻止。

我在谷歌上搜索了如何防止特定事件,但没有找到任何信息,所以我自己尝试了几种方法。然而,所有的案例都没有奏效。

  1. 添加return false;

    • 遗憾的是,return falseend 函数结束后停止所有剩余的事件。我的目标是只取消mousedown,而不是整个events。因此,我不能只将行放入函数中。
  2. mousedown 事件添加到off 方法中

    • 这也不起作用。属于off 方法的events 在关闭时仍然保留。因此,如果我在滑动元素后尝试拖动,则不会发生任何事情,因为 mousedown 事件现在已关闭。
  3. 添加preventDefault()stopPropagation()

    • 不能同案例1一样使用。
  4. 使用RegExp为火狐分离并制作新功能

    • 问题是触发了不需要的事件,而不是浏览器。

有什么方法可以取消这段代码中特定的event

class Evt {
  constructor($el) {
    this.$el = $el;
  }
  start(e) {
    console.log('start Function');
    this.$el.off('click');
    this.$el.on({
      ['touchmove mousemove']: (e) => this.eventmove(e),
      ['touchend mouseup']: (e) => this.end(e)
    });
  }
  eventmove(e) {
    e.preventDefault();
    console.log('move function');
    this.$el.on('click', (e) => {e.preventDefault();});
    return false;
  }
  end(e) {
    console.log('end function');
    this.$el.on('click');
    this.$el.off('touchmove touchend mousemove mouseup');
  }
  apply() {
    this.$el.on('touchstart mousedown', (e) => {
      this.start(e);
    })
  }
}
var thatEvt = new Evt($('#link'));
thatEvt.apply();
      a {
        width: 100%;
        height: 200px;
        border-radius: 10px;
        background-color: brown;
        font-family: Helvetica;
      }
    <a id="link" href="https://google.co.uk">
      Click/Touch and Drag/Swipe
    </a>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

【问题讨论】:

  • 您是在问如何注销事件处理程序?
  • @Rafael 如果这能解决我的问题,那么可以。抱歉,我现在无法解释更多,因为我的词汇量较少:/
  • @Rafael 如果注销使用的是off 方法,那么我已经尝试过了,这是问题中的情况2。
  • 我明白了...但是您要在每个 eventmove() 上注册点击事件侦听器?你为什么这样做?看来您应该删除该行。
  • @Rafael 如果我删除该行,则 dragging 该元素不会阻止重定向。它会打开一个新的浏览器。

标签: javascript click touch swipe drag


【解决方案1】:

据我所知,您的意图是在单击鼠标移动时不重定向链接。

首先,我假设您不希望链接可拖动,因此添加draggable="false" 标签。这将防止可能影响您已发送内容之外的代码的事件(例如重定向/删除链接)。

<a id="link" href="https://google.co.uk" draggable="false">
  Click/Touch and Drag/Swipe
</a>

如果这不能解决您的问题,我在下面整理了这段代码,它可能会更复杂,但应该更健壮一些。

class Evt {
  constructor($el) {
    this.$el = $el;
    this.active = false;
    this.preventRedirect = false;
  }
  start(e) {
    console.log('start Function');
    this.active = true;
    this.preventRedirect = false;
  }
  eventmove(e) {
    if (this.active) {
      console.log('move function');
      this.preventRedirect = true;
    }
  }
  end(e) {
    if (this.active) {
      console.log('end function');
      this.active = false;
    }
  }
  apply() {
    this.$el.on({
      ['touchstart mousedown']: (e) => this.start(e),
      ['click']: (e) => { //preventing onclick only if preventRedirect is set
        if (this.preventRedirect) {
          e.preventDefault();
          return false;
        }
      }
    });
    $("html").on({ //so that the mouse can move outside the element and still work.
      ['touchmove mousemove']: (e) => this.eventmove(e),
      ['touchend mouseup']: (e) => this.end(e)
    });
  }
}
var thatEvt = new Evt($('#link'));
thatEvt.apply();

【讨论】:

  • 啊,使用布尔标志的方式可能是解决我的问题的重要线索。我将使用您的示例对其进行测试和编辑。非常感谢您的回答:>
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多