2 回答
TA贡献1836条经验 获得超3个赞
您可以添加 在停用时更改为 true 的 ,并在它为 false 时允许递增:boolean
let myButton = document.getElementById('myButton');
let newCount = document.getElementById('clickCount');
let deact = document.getElementById('deactivate');
let count = 0;
let deactivated = false;
deact.addEventListener("click", function(){
deactivated = true;
});
myButton.addEventListener("click", function() {
if(!deactivated){
count++;
newCount.innerText = count;
}
});
<button id="myButton">Click me!</button>
<p>You clicked on the button <span id="clickCount">0</span> times</p>
<button id="deactivate">Désactivate counting</button>
TA贡献1848条经验 获得超10个赞
从单击其他按钮时删除事件侦听器#myButton
let myButton = document.getElementById('myButton');
let newCount = document.getElementById('clickCount');
let deact = document.getElementById('deactivate');
let count = 0;
function handler() {
count++;
newCount.innerText = count;
}
myButton.addEventListener('click', handler);
deact.addEventListener('click', () => myButton.removeEventListener('click', handler));
<button id="myButton">Click me!</button>
<p>You clicked on the button <span id="clickCount">0</span> times</p>
<button id="deactivate">Désactivate counting</button>
添加回答
举报