Home  >  Article  >  Web Front-end  >  JavaScript tutorial--click to cycle through pictures

JavaScript tutorial--click to cycle through pictures

巴扎黑
巴扎黑Original
2017-09-04 09:49:461866browse

This article introduces you to the effect of clicking to cycle through js through the example code. Friends who need it can refer to it

No more nonsense, I will directly post the code for you. The specific code is as follows:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>单击循环切换图片</title>
<script>
var i = 1;
function nextImg(){
i++;
//步骤2:获取页面元素
var next = document.getElementById("nImg");
//步骤3:更改元素的src属性
next.src = "img/"+i+".jpg";
if(i==3){
i=0;
} 
}
</script>
</head>
<body>
<p>
<!--步骤1:绑定onclick事件-->
<button onclick="nextImg()">下一张</button>
<br />
<!--需自己准备3张图片-->
<img src="img/1.jpg" id="nImg" width="600px" height="400px" />
</p>
</body>
</html>

The above is the detailed content of JavaScript tutorial--click to cycle through pictures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn