JS复制文本到剪切板
程序员文章站
2022-06-19 23:35:13
// 是否支持复制 export const isSupportCopy = ((!!document.queryCommandSupported) && document.queryCommandSupported('copy')); const copyTempElement = documen ......
// 是否支持复制
export const issupportcopy = ((!!document.querycommandsupported) && document.querycommandsupported('copy'));
const copytempelement = document.createelement('textarea');
copytempelement.setattribute('style', 'margin:0;padding:0;width:0;height:0;position:absolute;');
document.body.appendchild(copytempelement);
// 复制文本到剪切板
export function copytext(text) {
let succeeded;
try {
copytempelement.value = text;
copytempelement.select();
succeeded = document.execcommand('copy');
} catch (err) {
succeeded = false;
}
return succeeded;
}