当前位置:网站首页>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 变量 入门
- Logistic Regression VS Linear Regression
- 【HBZ分享】LockSupport的使用
- How to choose a technology stack for web applications in 2022
- Where is it safe to open an account for buying funds? Ask for guidance
- TSDB在民机行业中的应用
- Nr-arfcn and channel grid, synchronous grid and GSCN
- Dialogue: recommended system quick start route and summary of knowledge points
- Les neuf caractéristiques de la parole et les neuf temps en anglais
- 如何在 2022 年为 Web 应用程序选择技术堆栈
猜你喜欢

Complete and detailed compilation of experimental reports

shell 变量 入门

'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file

API encapsulation of uniapp applet

JVM uses tools to analyze classic cases of OOM

对白:推荐系统快速入门路线及各知识点总结

Beego--- notes

Logistic Regression VS Linear Regression

JVM 用工具分析OOM经典案例
![[untitled] the CMD command window displays' NPM 'which is not an internal or external command](/img/db/b1ae4b0d1110af1e24887ba3b4fe16.jpg)
[untitled] the CMD command window displays' NPM 'which is not an internal or external command
随机推荐
Settings the PC must be turned on
Classifier and cross entropy loss function
VGA display of de2-115 FPGA development board
Test your earning power? What will you do in the future?
Asp. Net webform exporting excel using npoi
Golang project dependency management tool go vendor, go Mod
shell 内置命令
[untitled]
Discriminative v.s.Generative
PubSub JS library realizes "cross component" data transfer
【世界历史】第二集——文明的曙光
Which is better and safer, GF easy gold rush or flush
广发易淘金和同花顺哪个更好,更安全一些
Renix perf: detailed explanation of IP network performance test tools and test case parameters
shell 字符串变量
Numpy库使用入门
哈希表、哈希冲突
测一测你的挣钱能力有多强?未来的你将从事什么职业?
Mise en place d'un Cluster kubernets avec plusieurs serveurs Cloud
BACnet gateway bl103 for building automation