javascript 删除 "onclick" 事件侦听器
javascript remove "onclick" event listener
我尝试了很多东西,但 none 都奏效了。
我想知道这是不可能的吗?
我知道使用 '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>
因为您没有使用 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
原来是,比如with
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>
您可以在 JS 中向元素添加一个 class,并在 CSS 中将 pointer-events
属性 设置为 none。
JavaScript
yourElement.classList.add("no-events");
CSS
.no-events{
pointer-events: none;
}
我尝试了很多东西,但 none 都奏效了。 我想知道这是不可能的吗? 我知道使用 '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>
因为您没有使用 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
原来是,比如with
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>
您可以在 JS 中向元素添加一个 class,并在 CSS 中将 pointer-events
属性 设置为 none。
JavaScript
yourElement.classList.add("no-events");
CSS
.no-events{
pointer-events: none;
}