<script>
function copySelectionText(){
var copysuccess // var to check whether execCommand successfully executed
try{
copysuccess = document.execCommand("copy") // run command to copy selected text to clipboard
} catch(e){
copysuccess = false
}
return copysuccess
}
function copyfieldvalue(e, id){
var field = document.getElementById(id)
field.select()
var copysuccess = copySelectionText()
}
var bio = document.getElementById('mybio')
bio.addEventListener('mouseup', function(e){
copyfieldvalue(e, 'mybio')
var copysuccess = copySelectionText() // copy user selected text to clipboard
}, false)
</script>
A pure JavaScript solution for copying the contents of a textarea to the clipboard when the user clicks on the textarea:
<script>
function copySelectionText(){
var copysuccess // var to check whether execCommand successfully executed
try{
copysuccess = document.execCommand("copy") // run command to copy selected text to clipboard
} catch(e){
copysuccess = false
}
return copysuccess
}
function copyfieldvalue(e, id){
var field = document.getElementById(id)
field.select()
var copysuccess = copySelectionText()
}
var bio = document.getElementById('mybio')
bio.addEventListener('mouseup', function(e){
copyfieldvalue(e, 'mybio')
var copysuccess = copySelectionText() // copy user selected text to clipboard
}, false)
</script>
发布评论
评论(1)
一个纯 JavaScript 解决方案,用于在用户单击文本区域时将文本区域的内容复制到剪贴板:
注意:如果您只想将文本区域的部分内容复制到剪贴板,请参阅教程 使用 JavaScript 读取所选文本并将其复制到剪贴板 有更多相关信息。
A pure JavaScript solution for copying the contents of a textarea to the clipboard when the user clicks on the textarea:
Note: If you want to copy only parts of the textarea contents to clipboard, the tutorial Reading and copying selected text to clipboard using JavaScript has more info on that.