【发布时间】:2012-11-22 01:05:47
【问题描述】:
我有一个搜索表单,可以将商家详细信息(每个商家的姓名、电话、电子邮件地址)输出到表格中。
我希望在每个字段旁边都有一个复制按钮,以便用户可以单击它并将其复制到剪贴板(复制时文本会突出显示)。我的用户将仅使用 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");
}
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>
我想知道这是否可能? ...
<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>
或者有没有更有效的方法来解决这个问题?
【问题讨论】:
-
忘记实际的一面,您想要一个网站来控制您的剪贴板吗?我会认为该网站很粗鲁,并且将来可能会避免使用它。我还想,如果他们(试图)这样做,他们还试图在我不知道的情况下做些什么。
标签: javascript