css html js 点击改变图片

在这里插入图片描述

<!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>
    <style>
        #pic {
            width: 300px;
            max-width: 0 auto;
            border: solid 1px red;
        }

        #pic input {
            margin-right: 45px;
        }
    </style>
</head>

<body>
    <div id="pic">
        <div class="img">
            <img id="img1" width="300" src="https://fc1tn.baidu.com/it/u=3749855832,2782074224&fm=202&mola=new&crop=v1"
                alt="">
        </div>
        <fieldset>
            <legend>选择按钮</legend>
            <input type="radio" name="pic" id="pic1" checked>
            <input type="radio" name="pic" id="pic2">
            <input type="radio" name="pic" id="pic3">
            <input type="radio" name="pic" id="pic4">
        </fieldset>
    </div>

</body>

</html>
<script>
    h1 = 'https://fc1tn.baidu.com/it/u=3749855832,2782074224&fm=202&mola=new&crop=v1'
    h2 = 'https://fc1tn.baidu.com/it/u=3945866729,4068990335&fm=202&mola=new&crop=v1'
    h3 = 'https://fc1tn.baidu.com/it/u=3653652530,1159058521&fm=202&mola=new&crop=v1'
    h4 = 'https://fc1tn.baidu.com/it/u=1167662336,2854872509&fm=202&mola=new&crop=v1'
    var arr = [h1, h2, h3, h4]
    img1 = document.getElementById("img1")
    rs = document.getElementsByName("pic")
    for (var i = 0; i < rs.length; i++) {
        rs[i].onclick = function () {
            index = Number(this.id.substring(this.id.length - 1, this.id.length)) - 1
            console.log(index)
            // img1.src = arr[index];
            img1.setAttribute('src', arr[index])
        }
    }
</script>
<!-- //https://fc1tn.baidu.com/it/u=3749855832,2782074224&fm=202&mola=new&crop=v1 -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值