Copying text of textarea in clipboard when button is clicked(单击按钮时在剪贴板中复制文本区域的文本)
问题描述
我正在寻找创建一个 jQuery(或 javascript)button
来选择 textarea
中的所有内容,然后将文本复制到您的 clipboard
当您单击按钮时.
I'm looking to create a jQuery (or javascript) button
that selects everything in a textarea
and then copies the text to your clipboard
when you clicked on button.
我发现了一些使用焦点事件的例子.但我正在寻找一个您实际上必须单击以进行选择和复制的按钮.
I have found some examples using the focus event. But I'm looking for a button that you actually have to click for the select and copy.
我该怎么做?
推荐答案
需要使用select()
选择 textarea
的文本并使用 execCommand('copy')
来处理选中的文本.它在高版本浏览器中工作.
You need to use select()
to selecting text of textarea
and use execCommand('copy')
to coping selected text. Its work in upper version of browsers.
$("button").click(function(){
$("textarea").select();
document.execCommand('copy');
});
您也可以不使用 jquery 来完成这项工作,如下所示
Also you can do this work without jquery as shown in bottom
document.querySelector("button").onclick = function(){
document.querySelector("textarea").select();
document.execCommand('copy');
}
document.querySelector("button").onclick = function(){
document.querySelector("textarea").select();
document.execCommand('copy');
};
<button>Select</button>
<br/>
<textarea></textarea>
这篇关于单击按钮时在剪贴板中复制文本区域的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单击按钮时在剪贴板中复制文本区域的文本


基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01