js 復制 標簽中的內容 方法


<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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM