简体   繁体   English

将多个 EventListener 设置为元素但删除单个元素

[英]Set multiple EventListener to element but delete single ones

I have an element, here in the example - $(window).我有一个元素,在这个例子中 - $(window)。 I would like to put several of the same EventListeners on this element, here "scroll" twice.我想在这个元素上放置几个相同的 EventListener,这里“滚动”两次。 These two EventListeners each perform different functions, which I cannot / do not want to connect with each other, shown here in a simplified manner.这两个 EventListener 各自执行不同的功能,我不能/不想相互连接,这里以简化的方式显示。 How can I delete individual EventListeners?如何删除单个 EventListener?

Here is the little example I made.这是我做的一个小例子。

 // Logs 1 when scrolling $(window).on("scroll", () => { console.log(1); }); // Logs 2 when scrolling $(window).bind("scroll", () => { console.log(2); }); // After 2 seconds stop logging 1 setTimeout(() => { $(window).unbind("scroll"); // remove only logging 1 }, 2000);
 #test { height:10000px; width:100vw; background:blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"></div>

You can use event.namespace for this and add multiple namespaces for scroll event like:您可以为此使用event.namespace并为滚动事件添加多个命名空间,例如:

$(window).on("scroll.event1", () => {
    console.log(1);
});

$(window).on("scroll.event2", () => {
    console.log(2);
});

and when you want to remove event 1, you can simply call .off on it like:当您想删除事件 1 时,您可以简单地调用.off ,如下所示:

setTimeout(() => {
  $(window).off("scroll.event1"); // remove only logging 1
}, 2000);

Demo:演示:

 $(window).on("scroll.event1", () => { console.log(1); }); $(window).on("scroll.event2", () => { console.log(2); }); // After 2 seconds stop logging 1 setTimeout(() => { $(window).off("scroll.event1"); // remove only logging 1 }, 2000);
 #test { height: 10000px; width: 100vw; background: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"></div>

You can add a .namespace to your event.您可以将.namespace添加到您的活动中。 This will still fire the same scroll event, but allows you to reference it with .off (or .unbind in your original code).这仍然会触发相同的scroll事件,但允许您使用.off (或原始代码中的.unbind )引用它。

Here's your code updated:这是您更新的代码:

 // Logs 1 when scrolling $(window).on("scroll.temp", () => { console.log(1); }); // Logs 2 when scrolling $(window).on("scroll", () => { console.log(2); }); // After 2 seconds stop logging 1 setTimeout(() => { $(window).off("scroll.temp"); // remove only logging 1 }, 2000);
 #test { height: 10000px; width: 100vw; background: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"></div>


More info from: https://api.jquery.com/on/更多信息来自: https://api.jquery.com/on/

Event names and namespaces事件名称和命名空间

Any event names can be used for the events argument.任何事件名称都可用于 events 参数。 jQuery will pass through the browser's standard JavaScript event types, calling the handler function when the browser generates events due to user actions such as click. jQuery 将通过浏览器的标准 JavaScript 事件类型,当浏览器由于用户操作(例如点击)产生事件时调用处理程序 function。

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

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