我的javascript打算在每次单击按钮时增加 i ,并从数组返回不同的结果。不知道为什么它只能部分工作。
<!DOCTYPE html>
<html>
<body>
<p id="demo">hi</p>
<img id="img" src="" alt="Pulpit rock" width="304" height="228"><br>
<button onclick="next()">next</button>
<script>
var myCars=["BMW","Toyota","Honda"];
var i=0;
function next() {
document.getElementById("demo").innerHTML=myCars[i]; //shows different text everytime
document.getElementById("img").alt=myCars[i]; //does not change everytime
i++;
}
</script>
</body>
</html>