Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 所有现代浏览器甚至IE9+都支持hat和。大多数web应用程序可能不再需要回退。可能的重复信用:此处和其中一个答案中的代码似乎取自:by@TimDown。他的工作示例见原文。在IE:window.clipboardData.setData(“text”,e_Javascript_Jquery_Html - Fatal编程技术网

Javascript 所有现代浏览器甚至IE9+都支持hat和。大多数web应用程序可能不再需要回退。可能的重复信用:此处和其中一个答案中的代码似乎取自:by@TimDown。他的工作示例见原文。在IE:window.clipboardData.setData(“text”,e

Javascript 所有现代浏览器甚至IE9+都支持hat和。大多数web应用程序可能不再需要回退。可能的重复信用:此处和其中一个答案中的代码似乎取自:by@TimDown。他的工作示例见原文。在IE:window.clipboardData.setData(“text”,e,javascript,jquery,html,Javascript,Jquery,Html,所有现代浏览器甚至IE9+都支持hat和。大多数web应用程序可能不再需要回退。可能的重复信用:此处和其中一个答案中的代码似乎取自:by@TimDown。他的工作示例见原文。在IE:window.clipboardData.setData(“text”,el.outerHTML)


所有现代浏览器甚至IE9+都支持hat和。大多数web应用程序可能不再需要回退。可能的重复信用:此处和其中一个答案中的代码似乎取自:by@TimDown。他的工作示例见原文。在IE:
window.clipboardData.setData(“text”,el.outerHTML)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);
                    document.execCommand('Copy');
                } catch (e) {
                    range.selectNode(el);
                    sel.addRange(range);
                    document.execCommand('Copy');
                }
            } else if (body.createTextRange) {
                range = body.createTextRange();
                range.moveToElementText(el);
                range.select();
                range.execCommand('Copy');

            }
        }
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");}
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();
}
document.execCommand("Copy");}
<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>
  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();
    }
    document.execCommand("Copy");
}
<script type="text/javascript">

function copytable(el) {
    var urlField = document.getElementById(el)   
    var range = document.createRange()
    range.selectNode(urlField)
    window.getSelection().addRange(range) 
    document.execCommand('copy')
}

</script>

<input type=button value="Copy to Clipboard" onClick="copytable('stats')">

<table id=stats>
    <tr>
        <td>hello</td>
    </tr>
</table>
<button class='btnclipboard' data-source='tableStudents'> Copy table </button>

<table id="tableStudents">
<thead>
 <th> user_id </th>
 <th> Name </th> 
</thead> 
<tbody>
 <tr>
  <td> 123 </td>
  <td> Proba </td>
 </tr>
<tbody>
</table>

<script>
    $('.btnclipboard').click(function(e) {
    e.preventDefault();
        copyTableToClipboard($(this).data('source'));
});

function copyTableToClipboard() {


var clipboard=new Clipboard('.btnclipboard',{

    text: function(trigger) {

        var txt=[];
                var headColumns = [];
                $("#tableStudents th").each(function(){
                    var textColumn = $(this).text();
                    if(textColumn == null || textColumn == "") {
                        textColumn = $(this).attr('title');
                    }
                    if(textColumn == undefined || textColumn == null) {
                        textColumn = "";
                    }
                    headColumns.push(textColumn);
                    // console.log($(this).text());
                });
                console.log('headColumns', headColumns);
                var head=headColumns;
                txt.push(head.join("\t"));

                var rowColumns=[];
                $("#tableStudents tr").each(function(){
                    var row=[];
                    $(this).find('td').each(function(){
                        var textColumn = $(this).text();
                        if($(this).find("i").hasClass('fa-check')){
                            textColumn = "1";
                        }
                        // if(textColumn == "") {
                        //  // textColumn = $(this).attr('title');
                        //  textColumn = "";
                        // }
                        // if(textColumn != null) {
                            row.push(textColumn);
                        // }
                    //console.log($(this).text());
                    });
                    if(row.length > 0) {
                        rowColumns.push(row);
                        txt.push(row.join("\t"));
                    }
                });
                console.log('rowColumns', rowColumns);
                return txt.join("\n");
    }


});

clipboard.on('success', function(e) {

    console.info('Action:', e.action);
    e.clearSelection();

    if (Notification.permission == "granted")
    {
        var notification = new Notification('Data copied to clipboard', {
            icon: '../dist/img/favicon.png',
            body: "You can now paste (Ctrl+v) into your favorite spreadsheet editor !"
        });
    }else{
        console.warn(Notification.permission);
    }
});

clipboard.on('error', function(e) {
    console.error('Action:', e.action);
    console.error('Trigger:', e.trigger);
});
}
</script>