Ai
1 Star 0 Fork 12

NextZero/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
copyToClipboard.js 833 Bytes
一键复制 编辑 原始数据 按行查看 历史
import createElement from './createElement'
/**
* 复制文本到粘贴板
* =============================================================
* @method copyToClipboard
* @since 1.4.0
* @param {String} str - 要复制的文本
*/
const copyToClipboard = (str) => {
const el = createElement('textarea', {
value: str,
readonly: 'position:absolute;left:-99999px;',
style: ''
})
const $body = document.body
let selected
$body.appendChild(el)
selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false
el.select()
document.execCommand('copy')
$body.removeChild(el)
if (selected) {
document.getSelection().removeAllRanges()
document.getSelection().addRange(selected)
}
}
export default copyToClipboard
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/nextzero/dom.js.git
git@gitee.com:nextzero/dom.js.git
nextzero
dom.js
dom.js
main

搜索帮助