代码之家  ›  专栏  ›  技术社区  ›  Mister Jojo Łukasz Daniel Mastalerz

javascript删除“onclick”事件侦听器

  •  -1
  • Mister Jojo Łukasz Daniel Mastalerz  · 技术社区  · 6 年前

    我试过很多东西,但都不管用。 我想知道这是否不可能? 我知道“bind”的“normal”方式,但是箭头函数可读性更高,我更喜欢使用它们。

    为了更好地理解我的问题,我编写了这个示例代码,尽可能完整地说明了问题。

    class MyClass_XY {
    
        constructor(zID) {
            let ref = document.getElementById(zID);
            this.name = zID;
            this.Info = ref.querySelector('span');
            this._Bt_Plus = ref.querySelector('.plus');
            this._bt_Stop = ref.querySelector('.stop');
    
            this.Nbre = 0;
            // this.stop    = false; // I don't whant this, because this is a small sample code of something more complex
    
            this._Bt_Plus.onclick = e => this._f_Bt_Plus_click(e);
            this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
    
            /*
            this.fct_Ref = null;
            this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
            */
        }
    
        _f_Bt_Plus_click(e) {
            e.stopPropagation();
            console.log(e.target.innerText);
            this.Nbre++,
                this.Info.innerText = this.Nbre.toString();
        }
    
        _f_Bt_Stop_click(e) {
            e.stopPropagation();
    
            // this._Bt_Plus.removeEventListener('click', this.fct_Ref  , false ); // is OK, how to deal the other ?
    
            this._Bt_Plus.removeEventListener("click", this._f_Bt_Plus_click, true); // didn't work :/  
    
            console.log(this.name, '_Bt_Plus remove onclick ');
        }
    }
    
    var
    Ananas = new MyClass_XY('Pineapple'), // I am a frog
    Bananes = new MyClass_XY('Bananas');
    <p id='Pineapple'> pineapple <span>0</span>
        <button class="plus">+1 pineapple</button>
        <button class="stop">stop</button>
    </p>
    
    <p id='Bananas'> Bananas <span>0</span>
        <button class="plus">+1 Bananas</button>
        <button class="stop">stop</button>
    </p>
    1 回复  |  直到 6 年前
        1
  •  4
  •   CertainPerformance    6 年前

    因为你没有用 addEventListener , removeEventListener 不起作用-通过分配给 onclick ,只需分配 null 到 这个 点击 属性:

    this._Bt_Plus.onclick = null;
    

    class MyClass_XY {
    
      constructor(zID) {
        let ref = document.getElementById(zID);
        this.name = zID;
        this.Info = ref.querySelector('span');
        this._Bt_Plus = ref.querySelector('.plus');
        this._bt_Stop = ref.querySelector('.stop');
    
        this.Nbre = 0;
        // this.stop    = false; // I don't whant this, because this is a small sample code of something more complex
    
        this._Bt_Plus.onclick = e => this._f_Bt_Plus_click(e);
        this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
    
        /*
        this.fct_Ref = null;
        this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
        */
      }
    
      _f_Bt_Plus_click(e) {
        e.stopPropagation();
        console.log(e.target.innerText);
        this.Nbre++,
          this.Info.innerText = this.Nbre.toString();
      }
    
      _f_Bt_Stop_click(e) {
        e.stopPropagation();
    
        // this._Bt_Plus.removeEventListener('click', this.fct_Ref  , false ); // is OK, how to deal the other ?
    
        this._Bt_Plus.onclick = null;
    
        console.log(this.name, '_Bt_Plus remove onclick ');
      }
    }
    
    var
      Ananas = new MyClass_XY('Pineapple'), // I am a frog
      Bananes = new MyClass_XY('Bananas');
    <p id='Pineapple'> pineapple <span>0</span>
      <button class="plus">+1 pineapple</button>
      <button class="stop">stop</button>
    </p>
    
    <p id='Bananas'> Bananas <span>0</span>
      <button class="plus">+1 Bananas</button>
      <button class="stop">stop</button>
    </p>

    如果你 使用 添加事件侦听器 然后使用 删除事件侦听器 稍后,您必须具有对传入的同一函数的引用 添加事件侦听器 最初,例如

    this.plusHandler = e => this._f_Bt_Plus_click(e);
    this._Bt_Plus.addEventListener('click', this.plusHandler);
    

    然后

    this._Bt_Plus.removeEventListener("click", this.plusHandler);
    

    class MyClass_XY {
    
        constructor(zID) {
            let ref = document.getElementById(zID);
            this.name = zID;
            this.Info = ref.querySelector('span');
            this._Bt_Plus = ref.querySelector('.plus');
            this._bt_Stop = ref.querySelector('.stop');
    
            this.Nbre = 0;
            
            this.plusHandler = e => this._f_Bt_Plus_click(e);
            this._Bt_Plus.addEventListener('click', this.plusHandler);
            
            this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
    
            /*
            this.fct_Ref = null;
            this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
            */
        }
    
        _f_Bt_Plus_click(e) {
            e.stopPropagation();
            console.log(e.target.innerText);
            this.Nbre++,
                this.Info.innerText = this.Nbre.toString();
        }
    
        _f_Bt_Stop_click(e) {
            e.stopPropagation();
    
            // this._Bt_Plus.removeEventListener('click', this.fct_Ref  , false ); // is OK, how to deal the other ?
    
            this._Bt_Plus.removeEventListener("click", this.plusHandler);
    
            console.log(this.name, '_Bt_Plus remove onclick ');
        }
    }
    
    var
    Ananas = new MyClass_XY('Pineapple'), // I am a frog
    Bananes = new MyClass_XY('Bananas');
    <p id='Pineapple'> pineapple <span>0</span>
        <button class="plus">+1 pineapple</button>
        <button class="stop">stop</button>
    </p>
    
    <p id='Bananas'> Bananas <span>0</span>
        <button class="plus">+1 Bananas</button>
        <button class="stop">stop</button>
    </p>