【发布时间】:2020-07-06 20:49:13
【问题描述】:
我有一个元素,在这个例子中 - $(window)。我想在这个元素上放置几个相同的 EventListener,这里“滚动”两次。这两个 EventListener 各自执行不同的功能,我不能/不想相互连接,这里以简化的方式显示。如何删除单个 EventListener?
这是我做的小例子。
// 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>
【问题讨论】:
-
注意 bind/unbind 已被弃用并在内部使用 on/off
标签: javascript html jquery bootstrap-4