javascript DOM List事件addEventListener绑定 removeEventListener 掉?


 // @hack
    // 长按当前的数据元素颜色变深
    nodeTouchmoveStyle(sortDrag, typeId) {
      function add() {
        if (typeId === '7') {
          this.parentNode.style.background = '#E6E6E6'
          return
        }
        this.style.background = '#E6E6E6'
      }

      function remove() {
        if (typeId === '7') {
          this.parentNode.style.background = ''
          return
        }
        this.style.background = ''
      }
      if (sortDrag.length) {
        sortDrag.forEach(item => {
          // item.removeEventListener('touchstart', add.bind(item), false)
          // item.removeEventListener('touchmove', add.bind(item), false)
          // item.removeEventListener('touchend', remove.bind(item), false)

          item.addEventListener('touchstart', add.bind(item), false)
          item.addEventListener('touchmove', add.bind(item), false)
          item.addEventListener('touchend', remove.bind(item), false)
        })
      }
    },

image.png

阅读 1.9k
1 个回答

xxx.bind() 已经创建一个新的 Function 对象了,你得有个变量持有它才行,后面 remove 这个变量,否则每次 bind 都是新对象。

let func1 = func.bind(something);
let func2 = func.bind(something);
func1 === func2; // false
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题