Copy function of h5

js+html5 implements copy text button

<div>
        <input type="text" name="guanfangaddress" id="guanfangaddress"/>
         <span id="copyBT" >复制</span>
</div>

<script type="text/javascript">
    function copyArticle(event) {        
        const range = document.createRange();        
        range.selectNode(document.getElementById('guanfangaddress'));         
        const selection = window.getSelection();        
        if(selection.rangeCount > 0) selection.removeAllRanges();        
        selection.addRange(range);        
        document.execCommand('copy');        
        alert( "Copy successfully!" );      
    }       
    document.getElementById('copyBT').addEventListener('click', copyArticle, false);
</script>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324443198&siteId=291194637