html - 如何将复制按钮链接到表格中的单元格?
问题描述
我有一个复制按钮脚本:
function myFunction() {
var table = document.getElementById('myTable');
var copyText = table.rows[1].cells[0].innerHTML;
copyText.select();
document.execCommand("copy");
alert("Copied");
}
我的桌子:
<table id="myTable">
{% for resp in results %}
<tr>
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
<td><button onclick="myFunction()">Copy text</button></td>
</tr>
{% endfor %}
</table>
我希望按钮复制 td {{ resp }} /td 中的文本
解决方案
function myFunction(val, event) {
var inp = document.createElement('input');
document.body.appendChild(inp)
inp.value = val;
inp.select();
document.execCommand('copy', false);
inp.remove();
alert('copied');
}
<table id="myTable">
<tr>
<td>one</td>
<td><button onclick="myFunction('one')">Copy text</button></td>
</tr>
<tr>
<td>two</td>
<td><button onclick="myFunction('two')">Copy text</button></td>
</tr>
<tr>
<td>three</td>
<td><button onclick="myFunction('three')">Copy text</button></td>
</tr>
<tr>
<td>four</td>
<td><button onclick="myFunction('four')">Copy text</button></td>
</tr>
</table>
一个快速的解决方案是将text
函数中的(要复制的)作为 argu 传递。
ctrl + v
看看结果。
推荐阅读
- android - Recyclerview 属性未显示
- python-3.x - 两个excel表的比较和写输出
- ios - 如果我撤销分发证书,现有版本会怎样?
- handlebars.js - 赛普拉斯脚本因 ReferenceError 失败:未定义把手
- java - Java:如何从包含 XML 字符串的文本区域中提取数据
- java - 在 Java 中实时读取大型日志文件
- linux - 我如何使用 shell 脚本减去 2 个时间戳
- javascript - 根据条件为Odoo10中的复选框添加颜色
- android - 在android中制作通知不会崩溃
- ios - WKWebView.scrollView.bounces = false 不工作