js 监听和移除监听事件

先上代码

const bfscrolltop = document.body.scrollTop; 
document.getElementById('consultDes').addEventListener('focus', this.resetScoll, false)
document.getElementById('consultDes').addEventListener('blur', function() {
   document.body.scrollTop = bfscrolltop
   document.getElementById('consultDes').removeEventListener('focus', this.resetScoll, false)
      clearInterval(timer);
   }, false)
}

说明:
在这里插入图片描述
removeEventListener只能移除具名函数的监听,且方法名称后面不能带()

// 监听手机返回
window.addEventListener("popstate", goBack(), false);
function goBack () {
  if (this.showSelectlist) {
    this.showSelectlist = false
    window.history.pushState("","","#");
    window.location.href = window.location.origin + '/#/taskAdd'
  }
  console.log(window.location.href)
}

// 取消监听
window.removeEventListener("popstate", this.goBack, false);

你可能感兴趣的:(js 监听和移除监听事件)