js点击按钮,更换某个img图片,且能循环更换多张图片?
1个回答
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
window.onload = function(){
// 这个数组里放置图片路径
var imgs = ['1.jpg','2.jpg','3.jpg'];
var len = imgs.length;
var current = 0;
var target = document.getElementById('target');
var setImg = function(){
target.src = imgs[current];
current = ++current == len ? 0: current;
};
setImg();
document.getElementById('btn').onclick = function(){
setImg();
}
};
</script>
</head>
<body>
<div>
<button id ="btn">change</button>
</div>
<img id ="target" src="#" alt="">
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询