copy whole html table to clipboard javascript

前端 未结 8 1634
暗喜
暗喜 2021-01-05 06:58

I have write javascript to select the table but I want to now automaticaly copy it after the click of the button.Please help me.My javascript is like this.

f         


        
相关标签:
8条回答
  • 2021-01-05 07:34
    function selectElementContents(el) {
    var body = document.body, range, sel;
    if (document.createRange && window.getSelection) {
        range = document.createRange();
        sel = window.getSelection();
        sel.removeAllRanges();
        try {
            range.selectNodeContents(el);
            sel.addRange(range);
        } catch (e) {
            range.selectNode(el);
            sel.addRange(range);
        }
    } else if (body.createTextRange) {
        range = body.createTextRange();
        range.moveToElementText(el);
        range.select();
    
    
    
    
    
    }
    CopiedTxt = document.selection.createRange();
    CopiedTxt.execCommand("Copy");}
    
    0 讨论(0)
  • 2021-01-05 07:35

    by using clicpboard.js making it much easier. for more info, check: https://webdesign.tutsplus.com/tutorials/copy-to-clipboard-made-easy-with-clipboardjs--cms-25086

    <script src="//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.4.0/clipboard.min.js">
    (function(){
        new Clipboard('#copy-table-button');
    })();
    </script>
    
    <button class="btn" id="copy-table-button" data-clipboard-target="#table_results">Copy</button>
    
    
    <table id='table_results' >
    
    </table>
    
    0 讨论(0)
提交回复
热议问题