javascript - Select and copy table to clipboard - Stack Overflow

I want to select table without headers, and it works, but I cannot get it so, that it would copy to cli

I want to select table without headers, and it works, but I cannot get it so, that it would copy to clipboard.

Here's the page: /

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    ".dtd">
<html xmlns="" xml:lang="en" lang="en">
<head>
    <title>ECB kursid seisuga: 2011-04-01 </title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
<style type="text/css">
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
</style>
<script type="text/javascript">
    function selectElementContents(el) {
        var body = document.body, range, sel;
        if (body.createTextRange) {
            range = body.createTextRange();
            range.moveToElementText(el);
            range.select();
            range.execCommand('Copy');
        } else if (document.createRange && window.getSelection) {
            range = document.createRange();
            range.selectNodeContents(el);
            sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
            sel.execCommand('Copy');
        }

    }
</script>
</head>

<body>
<table cellpadding="2">
<thead>

    <tr>
        <th>Valuuta</th>
        <th>Kurss</th>
    </tr>
</thead>
<tbody id="currencies">
<tr><td>USD</td><td>1,4141</td></tr><tr><td>JPY</td><td>118,56</td></tr><tr><td>DKK</td><td>7,4564</td></tr><tr><td>GBP</td><td>0,88150</td></tr><tr><td>NOK</td><td>7,8055</td></tr><tr><td>RUB</td><td>40,1500</td></tr><tr><td>CAD</td><td>1,3686</td></tr></tbody>

</table>
<input type="button" value="select table"
   onclick="selectElementContents( document.getElementById('currencies') );">
</body>
</html>

I want to select table without headers, and it works, but I cannot get it so, that it would copy to clipboard.

Here's the page: http://tuudik.lohv.eu/Asjad/EURXML/

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ECB kursid seisuga: 2011-04-01 </title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
<style type="text/css">
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
</style>
<script type="text/javascript">
    function selectElementContents(el) {
        var body = document.body, range, sel;
        if (body.createTextRange) {
            range = body.createTextRange();
            range.moveToElementText(el);
            range.select();
            range.execCommand('Copy');
        } else if (document.createRange && window.getSelection) {
            range = document.createRange();
            range.selectNodeContents(el);
            sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
            sel.execCommand('Copy');
        }

    }
</script>
</head>

<body>
<table cellpadding="2">
<thead>

    <tr>
        <th>Valuuta</th>
        <th>Kurss</th>
    </tr>
</thead>
<tbody id="currencies">
<tr><td>USD</td><td>1,4141</td></tr><tr><td>JPY</td><td>118,56</td></tr><tr><td>DKK</td><td>7,4564</td></tr><tr><td>GBP</td><td>0,88150</td></tr><tr><td>NOK</td><td>7,8055</td></tr><tr><td>RUB</td><td>40,1500</td></tr><tr><td>CAD</td><td>1,3686</td></tr></tbody>

</table>
<input type="button" value="select table"
   onclick="selectElementContents( document.getElementById('currencies') );">
</body>
</html>
Share Improve this question edited Jul 21, 2017 at 18:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 3, 2011 at 12:40 MarkoMarko 1,6274 gold badges24 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

This works for me on IE8:

    var table = document.getElementById('copyHtmlToClipboard');
    // Below line is essential !!!
    table.contentEditable = 'true';   

    var controlRange = document.body.createControlRange();
    controlRange.addElement(table);
    controlRange.execCommand("Copy");

In most browsers it isn't possible to copy to the system clipboard. To do this you need to use a hack. The most mon way is to use Flash. ZeroClipboard does this and appears to work quite well.

By the way, execCommand() is a method of document and TextRange objects, not Selection objects, so sel.execCommand("Copy") couldn't possibly work.

UPDATE

I've never actually used ZeroClipboard. Having looked at the docs, it doesn't look like it does what I had hoped (there seems to be no way to copy rich text) and is even more of a gruesome hack than I thought. You could copy the table's content as text via innerHTML using ZeroClipboard, but whether this is acceptable or not depends on what you're hoping the user can do with the copied content.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745243394a4618299.html

相关推荐

  • javascript - Select and copy table to clipboard - Stack Overflow

    I want to select table without headers, and it works, but I cannot get it so, that it would copy to cli

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信