document.execCommand ——js 原生 copy
<template>
<el-button type="primary" @click="copy" >复制</el-button>
</template>
...
...
<script>
export default {
methods: {
// js 原生 copy
copy(){
var input = document.createElement('input');
input.setAttribute('readonly', 'readonly');
input.setAttribute('value', this.address);
document.body.appendChild(input);
input.select();
input.setSelectionRange(0, 9999);
document.execCommand('Copy');
if(document.execCommand('Copy')) {
this.$message({
type: 'success',
message: '复制成功!'
});
}
document.body.removeChild(input); //单页应用务必加上
}
}
}
本文介绍了如何使用JavaScript的DOM API来实现文本复制功能,通过创建input元素、设置属性、选择并执行copy命令,展示了基础的前端开发技巧。点击按钮即可复制指定内容,适用于教育和开发者日常实践。
2341

被折叠的 条评论
为什么被折叠?



