Home  >  Article  >  Web Front-end  >  JS click cycle to switch to play pictures

JS click cycle to switch to play pictures

php中世界最好的语言
php中世界最好的语言Original
2018-04-18 14:53:293450browse

This time I will bring you JS click to cycle to switch to play pictures , what are the precautions for JS to click to cycle to switch to play pictures, the following is a practical case, let's take a look take a look.

<!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>

                                                                                                                                        

Recommended reading:

Use js to implement intra-page jump


JS to implement multiple choice assessment system


The above is the detailed content of JS click cycle to switch to play 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