【发布时间】:2020-08-23 17:52:35
【问题描述】:
我正在尝试从表格单元格创建复制到剪贴板。这个想法是,当用户用他的名字将鼠标悬停在第一个表格列单元格上时,按钮将显示并且他可以复制它。有什么方法可以创建这个悬停复制按钮而不在<td> 标签中创建按钮。
function myFunction() {
var copyText = document.getElementById("table-cell");
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
<table class="table">
<thead>
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<td id="table-cell">Mark <button onclick="myFunction()">Copy text</button></td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
【问题讨论】:
-
如果不在表格中,用户应该点击哪里?
-
您可以隐藏按钮,当鼠标悬停在单元格上时,显示按钮
-
是的,但是如果我有 50 个带有名称的表格单元格,我必须为每个名称创建 50 个按钮,或者还有其他方法可以创建它。
-
你不需要单元格的id,使用按钮元素对象来获取它的兄弟的文本内容
标签: javascript html jquery css twitter-bootstrap