当前位置:网站首页>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
边栏推荐
- Test your earning power? What will you do in the future?
- PubSub JS library realizes "cross component" data transfer
- 2020-03-20
- Turtlebot+lms111+gmapping practice
- Kubernetes cluster construction of multiple ECS
- Hash table, hash conflict
- 英语中的九大词性与九大时态
- K-line diagram 24 classic diagrams (shadow)
- Application of TSDB in civil aircraft industry
- Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts
猜你喜欢

程序員為什麼要軟一點?

Windows下MySQL的安装和删除

The best screenshot tool in the world, color absorption tool snipaste

As a software testing engineer, how do you think to ensure software quality?

Kubernetes 理解kubectl/调试

shell 字符串变量

两种方法实现pycharm中代码回滚到指定版本(附带截图展示)

Settings the PC must be turned on

还没弄明白微服务数据架构事务管理+ACID+一致性+CAP+BASE理论吗,看完彻底解决疑惑

分类器与cross entropy loss函数
随机推荐
Why should programmers be softer?
算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行
Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
Application of TSDB in civil aircraft industry
Is it safe for tongdaxin to open a stock account
Les neuf caractéristiques de la parole et les neuf temps en anglais
How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?
About reconnection of STM32 using lan8720a plug-in network cable
【世界历史】第二集——文明的曙光
重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
分类器与cross entropy loss函数
Table de hachage, conflit de hachage
Is it safe for Guosen Securities to open an account?
JVM 用工具分析OOM经典案例
Does stream even have application advanced learning? As a programmer, you know what
JS component
'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file
Which is better and safer, GF easy gold rush or flush
深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
JS to verify whether the string is a regular expression