Updates an image gallery with new photos
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>版本3 - innerHTML + 函式參數化</title>
</head>
<body>
<div id="photoContainer"></div>
<br><br>
<button onclick="changePhoto(-1)">上一張</button>
<button onclick="changePhoto(1)">下一張</button>
<script>
const photos = ["http://1.bp.blogspot.com/-xdHGOG59LCI/UjawN7V-deI/AAAAAAAABb8/v3dHRriJkjo/s1600/EUCL%E4%B8%8A%E5%8F%B0%E9%A0%98%E7%8D%8E01.jpg",
"http://4.bp.blogspot.com/-UKB9Pburh8I/Ujawb7t3wGI/AAAAAAAABcE/2h3OMM1sb-0/s1600/Frocius+Android%E4%B8%8A%E5%8F%B0%E9%A0%98%E7%8D%8E01.jpg",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUJYAY6cEyvTthq9s3Pz3RCayzcOM7UVffF8cmCk0XZBaAz7O6tLktSa7e1_f7Ni_ZxZaD8p4tLzqzhqnHo8rnsKuYvhPflJr2mAZeOSayg3sHjXCrrOtJ7cqPP6OdZAEJsHYJ5QWPTXw/s640/R0010396.JPG"];
let index = 0;
const container = document.getElementById("photoContainer");
function updateView() {
container.innerHTML = '<img src="' + photos[index] + '" width="300">';
}
function changePhoto(step) {
const newIndex = index + step;
if (newIndex >= 0 && newIndex < photos.length) {
index = newIndex;
updateView();
}
}
updateView();
</script>
</body>
</html>