<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.tpqh {
width: 500px;
text-align: center;
padding: 10px;
margin: auto;
margin-top: 100px;
}
</style>
<script>
window.onload = function () {
let a1 = document.getElementById('a1')
let a2 = document.getElementById('a2')
let tp = document.getElementById('tp')
let index = document.getElementById('index')
// alert(index.innerHTML)
let haha = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg"]
let count = 0;
//下一张
a2.onclick = function () {
count++;
if (count == haha.length) {
count = 0;
}
tp.src = haha[count];
index.innerHTML = '这是第' + (count + 1) + '张,一共5张'
}
//上一张
a1.onclick = function () {
if (count == 0) {
count = haha.length - 1;
}
count--;
tp.src = haha[count];
index.innerHTML = '这是第' + (count + 1) + '张,一共5张'
}
}
</script>
</head>
<body>
<div class="tpqh">
<p id='index'>这是第1张一共5张</p>
<img id='tp' src="img/1.jpg" alt="">
<button id="a1">上一张</button>
<button id="a2">下一张</button>
</div>
</body>