如何删除然后添加$(窗口).scroll?我需要存储一个变量,并在一些事件后重用它。
// here i store my var $(window).scroll(function(){ myScroll = $(window).scrollTop() }); $("#itemUnbind").click(function(){ // here i need to remove the listener }); $("#itembind").click(function(){ // here i need to add listener again });
谢谢。
解决方法
您需要将函数存储在变量中,然后使用
off
删除它:
var scrollHandler = function(){ myScroll = $(window).scrollTop(); } $("#itembind").click(function(){ $(window).scroll(scrollHandler); }).click(); // .click() will execute this handler immediately $("#itemUnbind").click(function(){ $(window).off("scroll",scrollHandler); });