【发布时间】:2019-08-01 07:26:39
【问题描述】:
How to select and copy a table element on Microsoft Edge?
This works in Chrome, Firefox, and IE11. Is the below code deprecated?
Please help!!
<table id="testTable">
<thead>
<tr class="primary">
<th>Solution</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test 1</td>
<td>€912 000.00</td>
<td>€50 640.00</td>
<td>€1 363 837.50</td>
<td>€1 322 845.33</td>
<td>€10 800.00</td>
<td>€1 041 666.67</td>
<td>€416 666.67</td>
<td>€5 118 456.17</td>
</tr>
</tbody>
</table>
$("#copyAllBtn").off("click").on("click", function (event) {
var el = document.getElementById("resulttable");
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");
});
我知道复制在某些事件点击时有效,我已经更新了最新代码,我在点击复制按钮时调用复制功能。 该代码不会引发任何错误,但不会在 Edge 上执行任何操作。请提出一些修改建议。
【问题讨论】:
-
你在哪里打电话给
selectElementContents?控制台有什么错误吗? -
@ThumChoonTat 我已经用我的最新代码更新了问题,我已经在单击按钮时编写了复制代码。控制台中没有错误,它只是在边缘没有做任何事情(在 IE11 中工作正常)
标签: javascript html dom copy microsoft-edge