js網頁(移動端適用)複製文字

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div class="u_div"> <span>微信</span>
        <div id="wxnum">123456</div>
        <a href="javascript:void(0);" id="copyBtn">複製</a>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function copyArticle(event) { const range = document.createRange(); range.selectNode(document.getElementById('wxnum')); const selection = window.getSelection(); if(selection.rangeCount > 0) selection.removeAllRanges(); selection.addRange(range); document.execCommand('copy'); alert("複製成功!"); } document.getElementById('copyBtn').addEventListener('click', copyArticle, false); </script>
</html>
相關文章
相關標籤/搜索