单击复制

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

    .box,
    .codeNumber {
        width: 120px;
        margin: 30px auto;
    }

    #addName {
        width: 120px;
    }
</style>
</head> <body>
<div class="codeNumber">验证码是:
    <span>123</span>
</div>


<div class="box">
    <select name="addName" id="addName">
        <option value='0'>语文</option>
    </select>
</div>

<script>
    var codeNumber = document.querySelector('.codeNumber>span');
    codeNumber.onclick = function () {
        //新建inp框
        var input = document.createElement('input');
        //inp的值等于要复制的内容
        input.value = codeNumber.innerHTML;
        //inp挂在到body中
        document.body.appendChild(input);
        //选中inp中的值
        input.select();
        //进行复制
        document.execCommand('copy');
        //复制完成,清除inp
        input.remove();
        alert("复制成功!!!!!")
    }
</script>
</body>
posted on 2023-04-17 16:26  hqingxiang  阅读(111)  评论(0)    收藏  举报