js實現複製功能兼容ios

html:javascript

<div id="copyBT">這是要複製的1內容</div>

<a id="contentas">這是複製按鈕</a>

js:html

   function copyArticle() {
                const range = document.createRange();
                range.selectNode(document.getElementById('copyBT'));
                const selection = window.getSelection();
                if (selection.rangeCount > 0) selection.removeAllRanges();
                selection.addRange(range);
                document.execCommand('copy');
                alert("複製成功")
            }

            document.getElementById('contentas').addEventListener('click', copyArticle, false);
相關文章
相關標籤/搜索