如何使用JavaScript使重置按钮实际工作?
考虑以下代码:如何使用JavaScript使重置按钮实际工作?,javascript,html,dom,Javascript,Html,Dom,考虑以下代码: var animalSound = document.getElementById("animalSound"); 重置按钮: var resetButton = document.querySelector("#reset"); 函数my console表示该值为null: resetButton.addEventListener("click", function(){ alert(" clicked button"); }) 它从中挑选的动物:
var animalSound = document.getElementById("animalSound");
重置按钮:
var resetButton = document.querySelector("#reset");
函数my console表示该值为null
:
resetButton.addEventListener("click", function(){
alert(" clicked button");
})
它从中挑选的动物:
var animals = ["elephant", "dog", "monkey", "zebra",
"parakeet", "cat", "pig", "Guerilla",
"skunk", "Leapord" ]
var x = animals[Math.floor(Math.random()*animals.length)];
animalSound.textContent = x;
我会将选择随机动物的代码放入一个函数中,并在单击时调用它:
var resetButton=document.querySelector(“#reset”);
resetButton.addEventListener(“单击”,函数(){
setNewAnimal()
});
//它从中挑选的动物
动物变量=[“大象”、“狗”、“猴子”、“斑马”,
“长尾鹦鹉”、“猫”、“猪”、“大猩猩”,
“臭鼬”、“豹”];
//随机设置动物
函数setNewAnimal(){
var animalSound=document.getElementById(“animalSound”);
var x=动物[Math.floor(Math.random()*anists.length)];
animalSound.textContent=x;
}
setNewAnimal()代码>
重置
那么“实际工作”意味着什么:你想实现什么,出了什么问题?“我的控制台上的函数表示该值为空”可能与我试图设置的值重复,因此当我单击按钮时,它重置列表,游戏开始over@JonathanRys-祝贺你具备了读心术。:-)提供所有上下文的方法总是有用的。如果没有HTML,我们不知道您是否正确选择了元素。请选择我的答案作为正确答案。谢谢