js & click copy to clipboard



js & click copy to clipboard

input select

vanilla js

  1. window.copy only for Chrome Console
  2. document.execCommand("copy") & copyText.select()


clickGetNewsId() {
let that = this;
let newsID = document.querySelector(`[data-uid="newsId"]`);
if (newsID) {
let result = that.commonHandle.newsId || ``;
let input = newsID.lastElementChild;
input.addEventListener(`click`, (e) => {
// console.log(`e.target =`, e.target);
// console.log(`e.target.dataset =`, e.target.dataset);
// console.log(`e.target.value =`, e.target.value);
// that.clickCopyText(input);
// this.clickCopyText();
try {
if (result) {
input.select();
document.execCommand("copy");
that.$hMessage.success(`资讯 ID, 复制成功!`);
} else {
that.$hMessage.info(`资讯 ID 为空, 无法复制!`);
}
} catch (err) {
console.log(`click copy error =`, err);
that.$hMessage.error(`你的浏览器太古老了,暂时不支持点击复制的功能!`);
}
});
} else {
//
}
// if (newsID) {
// result = newsID.lastElementChild.value;
// // result = that.commonHandle.newsId;
// }
},
clickCopyText(input) {
let that = this;
let result = that.commonHandle.newsId || ``;
// console.log(`click copy!`, result);
// console.log(`window.copy`, window.copy);
// undefined
try {
if (input) {
input.select();
if (result) {
document.execCommand("copy");
that.$hMessage.sucess(`资讯 ID, 复制成功!`);
} else {
that.$hMessage.info(`资讯 ID 为空, 无法复制!`);
}
}
} catch (err) {
console.log(`click copy error =`, err);
that.$hMessage.error(`你的浏览器太古老了,暂时不支持点击复制的功能!`);
}
// try {
// if (window.copy) {
// if (result) {
// console.log(`window.copy!`);
// window.copy(result);
// that.$hMessage.sucess(`资讯 ID, 复制成功!`);
// } else {
// that.$hMessage.info(`资讯 ID 为空, 无法复制!`);
// }
// }
// } catch (err) {
// console.log(`click copy error =`, err);
// that.$hMessage.error(`你的浏览器太古老了,暂时不支持点击复制的功能!`);
// }
return result;
},


vue

  1. input must be ​​:disabled="false"​

<h-row class-name="common-handle-padding">
<h-col span="24">
<span class="audit-common-lable">资讯 ID</span>
<h-input
aria-placeholder="资讯 ID"
placeholder="请输入资讯 ID"
style="width: 80%"
ref="newsId"
data-uid="newsId"
v-model="commonHandle.newsId"
@on-change="onChangeInput(`newsId`)"
@on-enter="onChangeInput(`newsId`)"
:readonly="true"
:disabled="false">
</h-input>
</h-col>
</h-row>



getSelection & execCommand

window.getSelection().toString()


window.document.execCommand(`copy`);
// true

document.execCommand(`copy`);
// true