我試圖做一些非常簡單的事情,當我點擊我的蘋果圖像時,我應該在我的角色圖像(Anthony)和我的appleNumber div中顯示一個數字。我使用addEventListener,但似乎有錯誤(我在Chrome上)。代碼:我的addEventListener不起作用
<script>
var appleButton = document.getElementById("apple");
appleButton.addEventListener("click", showAnthonyText);
appleButton.addEventListener("click", showNumber);
function showAnthonyText() {
document.getElementById("anthonyDelishBubble").style.display='block';
}
function showNumber() {
document.getElementById("appleNumber").innerHTML = "1";
}
</script>
<body>
<div id="anthonyGameTitle">Feed Amicable Anthony Some Apples</div>
<div id="anthonyGameMainContainer">
<div id="anthonyAppleGame"><img src="AmicableAnthony.png" alt="AmicableAnthony" height="300" width="300"></div>
<div id="apple"><img src="apple.png" alt="apple" height="80" width="80"></div>
<div id="anthonyDelishBubble"><img src="delish.png" alt="delish" height="80" width="80"></div>
<div id="appleNumber"></div>
</div>
</body>
看看你的控制檯錯誤。您正在嘗試獲取並使用對尚未存在的元素的引用 –
因此,我該怎麼做?... – KikePeris