嗨,我有一个小网站,在点击事件后会不断刷新。我该如何防止这种情况发生?
附注:我只能使用纯JS。
HTML
</head>
<body>
<header>
<h1>Points: </h1>
<span id="score"></span>
</header>
<div class="container">
<div>
<a href="" onclick="addPoints(1)">
<img src="assets/graphics/Friis.png" />
</a>
</div>
</div>
<script src="assets/scripts/cookieClicker.js"></script>
</body>
</html>
JS
var points = 0,
score = document.getElementById("score");
function addPoints(increase) {
console.log('hit');
points += increase;
score.innerHTML = '' + points;
};
href
更改为href="javascript:;"
或传递event
并调用preventDefault()
。 - haim770