双击如何选择和复制多个值?

时间:2016-08-11 11:10:30

标签: javascript php jquery html css

我想选择多个,然后复制(ctrl + c)。我的代码是

<table id="tbl1" border="1">
<tr>
    <td>first</td>
    <td>second</td>
</tr>
<tr>
    <td>third</td>
    <td>4th</td>
</tr>
<tr>
    <td>5th</td>
    <td>6th</td>
</tr>
</table>

我的表格会显示为

______________
|first|second|
_____________
|third|4th   |
_____________
|5th  |6th   |
______________

如果我双击&#34;第二个&#34; ,&#34;第三&#34;和&#34;第五&#34;然后应该选择这3个单元格,然后我将使用ctrl + c将这些数据复制并粘贴到wordpad中,我尝试了dblclick但它只适用于firefox。

3 个答案:

答案 0 :(得分:2)

我做了这个片段(测试过:在Chrome和Firefox上工作):

&#13;
&#13;
function copyToClipboard(text) {
    var $temp = $('<input>');
    $('body').append($temp);
    $temp.val(text).select();
    document.execCommand('copy');
    $temp.remove();
}

$(function($) {
    var ctrlDown = false;
    var ctrlKey = 17;
    var cmdKey = 91;
    var cKey = 67;

    $(document).keydown(function(e) {
        // if (CTRL + C)
        if (ctrlDown && (e.keyCode == cKey)) {
            copyToClipboard(selection.join(' '));
            selection = [];
            return false;
        }
        if (e.keyCode == ctrlKey || e.keyCode == cmdKey) {
            ctrlDown = true;
        }
    }).keyup(function(e) {
        if (e.keyCode == ctrlKey || e.keyCode == cmdKey) {
            ctrlDown = false;
        }
    });

    var selection = [];
    $('#tbl1 td').dblclick(function() {
        selection.push(this.innerHTML);
        $('#copyingText').val(selection.join(', '));
    });
});
&#13;
td {
  padding: 10px;
  width: 50px;
  background-color: #555;
  text-align: center;
  color: #fff;
  border-radius: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="tbl1" border="1">
    <tr>
        <td>first</td>
        <td>second</td>
    </tr>
    <tr>
        <td>third</td>
        <td>4th</td>
    </tr>
    <tr>
        <td>5th</td>
        <td>6th</td>
    </tr>
</table>

<hr>
<input type="text" id="copyingText" value="">
<input type="text" placeholder="paste text here">
&#13;
&#13;
&#13;

参考文献:

答案 1 :(得分:0)

尝试添加事件列表器,以便像这样双击

 srcBox.addEventListener("dblclick", hiLite, false);

答案 2 :(得分:0)

您可以使用.dblclick()来实现此目标(https://api.jquery.com/dblclick/)。然后使用jQuery选择你需要的东西。