要使用JavaScript实现图片的切换功能,可以通过以下步骤来完成:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Switcher</title>
</head>
<body>
<img id="myImage" src="image1.jpg" alt="Sample Image">
<button onclick="switchImage()">Switch Image</button>
<script src="script.js"></script>
</body>
</html>let currentImageIndex = 0;
const images = ["image1.jpg", "image2.jpg", "image3.jpg"]; // 图片数组
function switchImage() {
const imageElement = document.getElementById('myImage');
currentImageIndex = (currentImageIndex + 1) % images.length; // 循环切换图片
imageElement.src = images[currentImageIndex]; // 更新图片源
}通过上述步骤和代码示例,可以实现一个简单的图片切换功能。如果有更多具体问题或需要进一步的优化,可以进一步探讨。
没有搜到相关的沙龙