从数据库复制到剪贴板文本输出

Zun*_*uno 5 javascript

我有一个搜索表单,可将​​商人详细信息(每个商人的姓名,电话,电子邮件地址)输出到表格中。
我希望在这些字段中的每个字段旁边都有一个复制按钮,以便用户可以单击它并将其复制到剪贴板(复制时文本突出显示)。我的用户将仅使用IE9浏览。

问题在于可能会有不止一组结果,因此脚本无法调用特定的编号函数,就像我对以下textarea所做的那样:

function highlightmetasearch01() {
    document.copydata01.text2copy01.select();
     document.copydata01.text2copy01.focus(); 
}
function copymetasearch01() {
    highlightmetasearch01();
    textRange = document.copydata01.text2copy01.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy");
}

function highlightmetasearch02() {
    document.copydata02.text2copy02.select();
    document.copydata02.text2copy02.focus(); 
}
function copymetasearch02() {
    highlightmetasearch02();
    textRange = document.copydata02.text2copy02.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy");
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<textarea name="text2copy01">The first textarea.</textarea>
<br />
<button onclick="copymetasearch01();return false;">COPY</button>

<textarea name="text2copy02">The second textarea.</textarea>
<br />
<button onclick="copymetasearch02();return false;">COPY</button>
Run Code Online (Sandbox Code Playgroud)

我想知道这是否可能?...

<td><span>Name from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>

<td><span>Phone from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>

<td>Other text here that shouldn't be highlighted or copied <span>Email address from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>
Run Code Online (Sandbox Code Playgroud)

还是有一种更有效的方法来解决这个问题?

Lev*_*lho 1

这个问题:

如何在 JavaScript 中复制到剪贴板?

...包含有关使用 JavaScript 将文本复制到剪贴板的相当长的讨论。获得最多支持的(在我看来是正确的)答案实际上并不进行复制,而是利用弹出窗口来显示一个文本框,其中包含已选择的文本,允许用户只需按 CTRL+C 即可进行复制。

其背后的原因是因为网站控制用户剪贴板上的内容可能是危险的,并且对于用户来说是不受欢迎的。了解您在这里获得了用户的许可,但仍然...如果您想采用上面帖子中建议的答案并将其应用到您的网站,也许可以包含一个按钮,该按钮会自动选择要复制到的文本旁边的字段。有关在字段中选择文本的信息,请参阅此文章:以编程方式选择输入字段中的部分文本