javascript删除“ onclick”事件监听器

时间:2018-11-18 03:19:21

标签: javascript es6-class

我尝试了很多事情,但是没有一个起作用。 我想知道这是否不可能吗? 我知道“绑定”的“正常”方式,但是箭头功能更具可读性,我更喜欢使用它们。

为了更好地理解我的问题,我制作了此示例代码来尽可能全面地说明问题。

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 个答案:

答案 0 :(得分:4)

因为您没有使用addEventListener添加监听器,所以removeEventListener不起作用-要通过分配给onclick来删除附加的监听器,只需将null分配给 再次使用onclick属性:

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>

如果您没有使用addEventListener,那么以后要使用removeEventListener,则必须对传递给addEventListener的同一函数进行引用最初,例如与

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>