這隻在啓動時運行一次,我不明白髮生了什麼。對不起,如果格式不好,這是我的第一個問題。Javascript代碼在啓動時運行一次(addEventListener不工作)
這是HTML代碼:
<!DOCTYPE html>
<html>
<head>
<title>Clicker</title>
</head>
<body>
<ul>
<li>Mouse</li>
<li>DangerMouse</li>
<li>Brain</li>
<li>Ratata</li>
</ul>
<img id="mouseElem" src="https://cdn.theatlantic.com/assets/media/img/photo/2015/11/images-from-the-2016-sony-world-pho/s01_130921474920553591/main_900.jpg?1448476701" alt="whoops" style="width:304ps;height:228px;">
<button type="button">Mouse1</button>
<button type="button">DangerMouse</button>
<button type="button">Brain</button>
<button type="button">Ratata</button>
<h2>Mouse</h2>
<p>Number of Clicks: </p>
<div id="presentClicks"><p></p></div>
<script src="js/javardscript.js"></script>
</body>
</html>
,這是JavaScript代碼
var clickerCounter = 0;
//document.getElementById('presentClicks').innerHTML = clickerCounter;
document.getElementById('presentClicks').addEventListener('click', addClick(clickerCounter));
document.getElementById('mouseElem').addEventListener('click', boogaFunction());
function boogaFunction() {
alert("booga");
}
function addClick(clickerCounter) {
clickerCounter++;
document.getElementById('presentClicks').innerHTML = clickerCounter;
}
'document.get ....('click',boogaFunction());'這裏不要調用boogaFunction,只是發送引用。 ie'('click',boogaFunction)' – rajuGT
[javascript addEventlistener「click」not working]可能重複(http://stackoverflow.com/questions/33906015/javascript-addeventlistener-click-not-working) – trincot