我尝试了很多东西,但都没有奏效。
我想知道这是不可能的吗?
我知道“绑定(bind)”的“正常”方式,但箭头函数更具可读性,我更喜欢使用它们。

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

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>

最佳答案

因为您没有使用 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>

关于javascript 移除 "onclick"事件监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53357598/

10-13 01:28