首页 > 技术文章 > js 复制 标签中的内容 方法

wjm956 2018-12-20 18:12 原文

<span id='id'>hello world</span><input type='button' onClick='copy("id")' value='点击复制内容' />

<script>
    function copy(id) {
        var Url2=document.getElementById(id).innerText;
        var oInput = document.createElement('input');
        oInput.value = Url2;
        document.body.appendChild(oInput);
        oInput.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        oInput.className = 'oInput';
        oInput.style.display='none';
        console.log("复制成功");
    }
</script>

标签的话  我用的是 span 用什么标签看需求了 只要有id 就可以复制出来标签中的value

推荐阅读