js copy the code (compatible with IOS, android) also apply to the PC

Directly on the code, by triggering .copybtn Copy button to copy the contents of .copytxt.

<div class="title">
	<a class="button" href="javascript:history.back();">返 回</a>
	作品编号:<span class="copytxt">PHP-001007</span> 
	<button class="copybtn">复 制</button>
</div>

<script>
    function copyNo(event) {
        const range = document.createRange();
        range.selectNode(document.querySelector(".copytxt"));

        const selection = window.getSelection();
        if(selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('Copy');
        alert("复制成功!");
    }
    document.querySelector(".copybtn").addEventListener('click', copyNo, false);
</script>

 

Published 12 original articles · won praise 4 · Views 2012

Guess you like

Origin blog.csdn.net/ba_qi/article/details/103935331