我有一个表,其中包含一些数据,当双击以选择/复制/粘贴时,该数据最终会在测试的两边带有一些空白(可能来自单元格内的其他一些项)。
我的解决方案是让一个按钮显示在文本的右侧,该按钮可在单击时复制文本。(由于jon-p,我可以在单元格中迭代这些按钮)
我的问题是如何添加复制新按钮所在单元格文本的函数。
此外,我无法直接编辑页面,因此我正在使用tampermonkey注入代码。
http://jsfiddle.net/pshock13/kcvbyq9r/
<table>
<thead>
<th>Tools</th>
<th>Shipment</th>
<th>Barcode</th>
<th>More Info</th>
</thead>
<tbody>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=123456789">123456789</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=asdfghjkl">asdfghjkl</a>
</div>
</td>
<td>
<div class="relative">
<span>9870356542</span>
</div>
</td>
</tr>
<tr>
<td><span>✔ ✘</span></td>
<td>
<div class="relative">
<a href="something.com/Search?searchKey=987654321">987654321</a>
</div>
</td>
<td>
<div class="relative">
<a href="na.something.com/results?s=qwertyuiop">qwertyuiop</a>
</div>
</td>
<td>
<div class="relative">
<span>asfg456sdfg</span>
</div>
</td>
</tr>
</tbody>
</table>
var copyBtn = "<span class='copy' onClick='copyText()'>📋</span>"
var shipmentCells = document.querySelectorAll("tbody tr > td:nth-child(2) > div");
for(var i = 0; i < shipmentCells.length; i++){
//Append the new element to the innerHTML
shipmentCells[i].innerHTML += copyBtn;
}
答案 0 :(得分:1)
您可以使用the GM_setClipboard()
function简化操作。
重要:
onclick
。.innerHTML
is also poor practice-在用户脚本中加倍。@require
一起使用时,使用jQuery几乎没有任何缺点,并且在编码的简便性,速度和简便性方面都有很多收获。这是完整的有效用户脚本,可添加并激活复制按钮。我添加了一些可选的格式和UI,仅用于咯咯笑:
// ==UserScript==
// @name _Add copy buttons to a table
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @match https://output.jsbin.com/vuyewal
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_addStyle
// @grant GM_setClipboard
// ==/UserScript==
/* global $ */
/* eslint-disable no-multi-spaces */
//-- Add copy button to column 2:
$("td:nth-child(2) > div.relative").after (`<span class='tmCopyBtn'>📋</span>`);
//-- Style it:
GM_addStyle ( `
.tmCopyBtn { cursor: pointer; }
/* Also tweak the div style: */
td:nth-child(2) > div.relative { display: inline-block; margin-right: 1ex;}
/* Also add blinker effect for better UI: */
.justCopied { animation: blinkYellow 1s ease-out 2; }
@keyframes blinkYellow {
50% { background-color: yellow; }
}
` );
//-- Activate it:
$("table").on ("click", ".tmCopyBtn", zEvent => {
//-- Get text of adjacent <div> and strip leading/trialing whitespace:
var targetDiv = $(zEvent.target).prev ("div.relative");
var textToCopy = targetDiv.text ().trim ();
GM_setClipboard (textToCopy, "text/plain");
//-- Feedback to user:
$(".justCopied").removeClass ("justCopied");
targetDiv.parent ().addClass ("justCopied");
} );
任何人都可以针对this target page at JS Bin进行测试。