当前位置:网站首页>Extend JS copy content to clipboard
Extend JS copy content to clipboard
2022-06-25 14:24:00 【Fat goose 68】
List of articles
One 、 Reference resources
Two 、 principle
- Dynamically create textarea label , And set up readOnly Properties and move out of the visual area
- Assign the value to be copied to textarea Labeled value attribute , And insert into body
- Selected value textarea And copy
- take body Inserted in textarea remove
- Bind the event on the first call , Remove events when unbinding
3、 ... and 、 Code implementation
- Reference resources document.execCommand MDN
- Concept when a HTML When the document switches to design mode ,document expose execCommand Method , This method allows you to run commands to manipulate
Elements of the editable content area. - Common commands
- copy Copy the current selection to the clipboard . The conditions for enabling this feature vary from browser to browser , And different periods , The enabling conditions are also different . Please check the browser compatibility table before using , To determine if it is available .
- cut Clip the currently selected text and copy it to the clipboard . The conditions for enabling this feature vary from browser to browser , And different periods , The enabling conditions are also different . Please check the browser compatibility table before using , To determine if it is available .
- delete Delete the selected part .
Four 、 Code implementation
4.1 Ordinary HTML In the code
<button onclick="copyToClip(' Content ')">Copy</button>
<script> /** * Copy a single line of content to the pasteboard * content : Content to be copied * message : Tips after copying , If not, the default prompt is " Replication success " */ function copyToClip(content, message) {
// Dynamically create textarea / input label var aux = document.createElement("input"); aux.setAttribute("value", content); // Insert into body document.body.appendChild(aux); // Selected value textarea And copy aux.select(); document.execCommand("copy"); document.body.removeChild(aux); if (message == null) {
alert(" Replication success "); } else {
alert(message); } } </script>
4.2 vue in v-copy Instructions
const copy = {
bind(el, {
value }) {
el.$value = value
el.handler = () => {
if (!el.$value) {
// When the value is empty , Give hints . According to the project UI Design carefully
console.log(' No copy ')
return
}
// Dynamically create textarea label
const textarea = document.createElement('textarea')
// Will be textarea Set to readonly prevent iOS I'll call up the keyboard automatically , At the same time textarea Move out of the visible area
textarea.readOnly = 'readonly'
textarea.style.position = 'absolute'
textarea.style.left = '-9999px'
// will copy The value is assigned to textarea Labeled value attribute
textarea.value = el.$value
// take textarea Insert into body in
document.body.appendChild(textarea)
// Select values and copy
textarea.select()
const result = document.execCommand('Copy')
if (result) {
console.log(' Replication success ') // According to the project UI Design carefully
}
document.body.removeChild(textarea)
}
// Bind click event , It's a key copy La
el.addEventListener('click', el.handler)
},
// Triggered when the value passed in is updated
componentUpdated(el, {
value }) {
el.$value = value
},
// When an instruction is unbound to an element , Remove event binding
unbind(el) {
el.removeEventListener('click', el.handler)
},
}
export default copy
边栏推荐
- Shell built-in commands
- Two common ways for orcale to clear table data
- 程序員為什麼要軟一點?
- laravel8实现图片验证码
- 中国电池技术取得重大突破,日韩美都落后了,中国巩固了领先优势
- How to view the Chrome browser plug-in location
- 第一次读 “Clean” 系列,并没有觉得这是一本多好的书
- ‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
- BACnet gateway bl103 for building automation
- Asp. Net webform exporting excel using npoi
猜你喜欢

Settings the PC must be turned on

Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk

Beego--- notes

Variables, scopes, and variable promotion

Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud

完整详细的汇编实验报告

Network remote access using raspberry pie

英語中的九大詞性與九大時態

Realization of neural networks with numpy

Shell built-in commands
随机推荐
Renix Perf: IP网络性能测试工具及测试用例参数详解
Two common ways for orcale to clear table data
Let and const commands
Cool assembly tutorial - concise x86 assembly language tutorial (1)
Custom instruction, mixing, routing, lifecycle
权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?
Logistic Regression VS Linear Regression
专家建议|8大措施加速你的创新职业规划和成长
Kubernetes 理解kubectl/调试
Is qiniu regular? Is it safe to open a stock account?
How to choose a technology stack for web applications in 2022
电脑必须打开的设置
Settings the PC must be turned on
超酷汇编教程-- 简明x86汇编语言教程(1)
一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk
TSDB在民机行业中的应用
Shell string variable
About reconnection of STM32 using lan8720a plug-in network cable
现在股票开户用什么app最安全?知道的给说一下吧
Getting started with numpy Library