我有一个HTML文件和一个JavaScript文件。在HTML中,我的按钮带有onlclick = "myFunction
,然后在JS文件中,我具有myFunction
。但是,当我尝试在流星中运行该应用程序时,它说该功能未定义。显然,与该功能相关的所有内容都应该在JS文件中,而我应该在HTML文件中删除onclick = "myFunction
。
我尝试了许多方法,例如addEventListener
或object.onlclick
,但没有任何效果。
<div class="container">
<div id= "step1">Identify 5 things you see</div>
<button class= "btn" id="button1" onclick= "myFunction(1)">Button1</button>
<button class= "btn" id="button2" onclick= "myFunction(2)">Button2</button>
<button class= "btn" id="button3" onclick= "myFunction(3)">Button3</button>
<button class= "btn" id="button4" onclick= "myFunction(4)">Button4</button>
<button class= "btn" id="button5" onclick= "myFunction(5)">Button5</button>
</div>
<script>
function myFunction(buttonNumber) {
answered["button" + buttonNumber] = true;
var allTrue = Object.keys(answered).every(function(key){ return answered[key] === true });
if (allTrue) {
document.getElementById("button" + numberOfButtons).style.display = "none";
if (compteur == 0){
document.getElementById("step1").innerHTML = "Identify 4 things you see";
} else if (compteur == 1){
document.getElementById("step1").innerHTML = "Identify 3 things you see";
} else if (compteur ==2){
document.getElementById("step1").innerHTML = "Identify 2 things you see";
} else if (compteur ==3){
document.getElementById("step1").innerHTML = "Identify 1 things you see";
}
delete answered["button" + numberOfButtons];
Object.keys(answered).forEach(function(key){ answered[key] = "false" });
numberOfButtons--;
compteur ++;
};
};
}
</script>
应该是这样的:单击5个按钮时,一个消失,文本改变,然后单击4个按钮,直到没有按钮为止都发生同样的事情。