【问题标题】:Copy to clipboard from table cell从表格单元格复制到剪贴板
【发布时间】: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


【解决方案1】:

你可以试试这个..也许它会帮助你..

通过悬停从表格单元格复制文本..

document.querySelectorAll(".table-cell").forEach(function(elm){
elm.addEventListener("mouseover", function(e){
 e.target.style.backgroundColor = 'red'; 
  var copyText = e.target.textContent; 
   const el = document.createElement('textarea');
  el.value = copyText;
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);

 
  /* Alert the copied text */
  alert("Copied the text: " + el.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 class="table-cell">Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <td class="table-cell">Jacob </td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <td class="table-cell">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">

【讨论】:

  • 是的,这很好,但用户需要点击按钮来复制名称。
  • 您写的问题是,当您悬停时,表格单元格将被复制...
  • 是的,对不起,我的错,悬停后按钮会显示。我会编辑它。
  • @Sauce btw 用 click 事件替换 moueover 事件...根据需要自行设计...也许主要问题已解决
  • 如果你可以查看我的代码:为什么如果我点击按钮它不会复制 td 的内容? codepen.io/S4UCY/pen/abNpyWB
【解决方案2】:

我自己可以提供这样的解决方案。由于您没有具体要求,因此我将自行决定解决方案。

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-cell {
  position: relative;
}

#table-cell p{
   display: none;
   position: absolute;
   font-size: 10px;
   margin: 0;
   top: 0;
   cursor: pointer;
   color: blue;
   transition: .3s;
}

.table tr:hover #table-cell p {
   display: block;
}

.table #table-cell p:hover {
   transform: scale(1.3);
}
<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 <p onclick="myFunction()">Copy text</p></td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>

      <td id="table-cell">Jacob <p onclick="myFunction()">Copy text</p></td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <td id="table-cell">Larry <p onclick="myFunction()">Copy text</p></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">

【讨论】:

  • 主要思想是将鼠标悬停在连续第一个表格单元格(有名字)后复制内容。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
相关资源
最近更新 更多