例如,我有一个跨度:
<span class="alphStyle" id="a1">a</span>
<span class="alphStyle" id="a2">b</span>
<span class="alphStyle" id="a3">c</span>...
......等等
点击功能(完美无缺):
var on = document.querySelectorAll(".alphStyle");
for (var i = 0; i < on.length; i++){
on[i].addEventListener('click', function(){
var whatLetter = this.innerHTML;
var num = 0;
var trueCount = 0;
for (num = 0; num < guessWord.length; num++) {
if (whatLetter == guessWord.charAt(num)) {
secretW.getElementsByClassName("lineStyle")[num].innerHTML = whatLetter;
this.style.backgroundColor = "rgb(65,208,65)";
trueCount++;
trueCount2++;
}
}
if (trueCount == 0) {
mistake();
this.style.backgroundColor = "rgb(255,100,100)";
}
this.disabled = true;//this one doesn't work
});
}
它完全正常,除了我希望它禁用我已经点击的跨度。可能吗?我应该在这里添加什么代码?
答案 0 :(得分:0)
如果您的目标是阻止未来点击触发点击处理程序,则可以使用removeEventListener()
删除点击处理程序。为此,您需要将侦听器分配给稍后可以引用的变量。
var on = document.querySelectorAll(".alphStyle");
var handler = function(){
// ...
this.removeEventHandler('click', handler);
};
for (var i = 0; i < on.length; i++){
on[i].addEventListener('click', handler);
}
答案 1 :(得分:0)
单击跨度后,您可以设置/检查数据属性。
public class Etudiant {
private String nom , prenom ;
private int anneeNais, LIMITE=60;
private double note;
private String code ;
以下是一个工作示例https://jsfiddle.net/yd7w62e6/1/