gpt4 book ai didi

javascript 删除 "onclick"事件监听器

转载 作者:行者123 更新时间:2023-11-30 07:31:05 26 4
gpt4 key购买 nike

我尝试了很多方法,但都没有用。我想知道这是不可能的吗?我知道“绑定(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com