这次的web作业中的一个小知识点,在页面中使用js,使得页面有更多功能。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片切换</title>
    <style type="text/css">
        .tab{
            width: 1080px;
            height: 400px;
            margin: 0 auto;
            background-color: #ccc;
        }
        img{
            width: 1080px;
            height: 400px;
        }
    </style>
</head>
<body>
    <div class="tab" id="tabe">
        <img src="img/p1.jpg" id="img" />
    </div>
</body>
<script>
    //使用数组保存图片的地址
    var imgs=["img/p1.jpg","img/p2.jpg","img/p3.jpg","img/p4.jpg","img/p5.jpg"];
    var index=0;
    function qiehuan(){
        document.getElementById("img").src=imgs[index];
        index++;
        if(index > 4){
            index=0;
        }
    }
    setInterval("qiehuan()",3000);
</script>
</html>

在使用到的地方加入id = img即可。