js变换显示图片的实例


<head>
    <title></title>
   <script type="text/javascript">
       function UpdateImg() {
           //document.getElementById('img1').setAttribute('src', 'images/2.jpg');
           var dogandcat = document.getElementsByName('dogandcat'); //获取所有name=dogandcat的单选按钮
           for (var i = 0; i < dogandcat.length; i++) {
               if (dogandcat[i].checked == true) {
                   var value = dogandcat[i].getAttribute('value');
                   switch (value) {
                       case '2':
                           document.getElementById('img1').setAttribute('src', 'images/2.jpg');
                           break;
                       case '3':
                           document.getElementById('img1').setAttribute('src', 'images/3.jpg');
                           break;
                       case '4':
                           document.getElementById('img1').setAttribute('src', 'images/4.jpg');
                           break;
                       case '6':
                           document.getElementById('img1').setAttribute('src', 'images/6.jpg');
                           break;
                   }
               }
           }
       }
    </script>

</head>
<body>
 <img src="https://www.jb51.net/images/9.jpg" />
    <br />
    <input type="button"  value="改变图片" />
    <br />
    <input type="radio" value="2"  />忧伤的小狗
    <input type="radio" value="3"/>卖萌的小狗
    <input type="radio" value="4"/>愤怒的小狗
    <input type="radio" value="6"/>可爱的小猫
</body>

下面是程序运行时的界面:点击不同的按钮图片是不一样的

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdgxxd.html