触发shareQuestions()实现一键复制。input只能单行,textarea可以多行。chrome中运行代码,如果input、textarea的样式是display:none或者visibility: hidden;或者input的type=”hidden”会复制不到内容,其他浏览器没测。
<textarea style="opacity: 0;width: 0; height: 0; position: fixed; left: -10000px;" id="copyQuestionValue"></textarea>
或者
<input type="text" style="opacity: 0;width: 0; height: 0; position: fixed; left: -10000px;" id="copyQuestionValue" />
var shareQuestions = function(){
try {
var text = "获取复制文本第1行"+ "\n" + "获取复制文本第2行"+ "\n";
var textarea = document.getElementById("copyQuestionValue");
textarea.value = text; // 修改文本框的内容
textarea.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
console.log('复制成功');
} catch (e) {
console.log('复制失败');
}
}