点击按钮切换图片

点击按钮在两张图片之间切换.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="button" value="点击" id="btn"><br>
    <img src="images/a.jpg" id="pic">
    
    <script>
        //获取元素
        var btn = document.getElementById("btn");
        var pic = document.getElementById("pic");

        var num =1;
        btn.onclick = function(){
            
            if(num == 1){
                pic.src = "images/b.jpg";
                num = 0;
            }else {
                pic.src = "images/a.jpg";
                num = 1;
            }
            
        };
    </script>
</body>
</html>
posted @ 2021-08-12 17:08  charonmomo  阅读(66)  评论(0编辑  收藏  举报