首页 > 解决方案 > 单击Jquery中的按钮时从本地存储复制字符串

问题描述

当我单击按钮并且 text.select() 函数对字符串不起作用时,我想复制字符串。

谁能告诉我我该怎么做

//getting the text from local storage
let text = window.localStorage.getItem('content');//return string

//select the text
text.select();//this give the error because this only accept the HTML collection

//range
text.setSelectionRange(0, 999999999);
//copy command
document.execCommand("copy");

标签: javascriptjquery

解决方案


使用剪贴板 API

const text = window.localStorage.getItem('content');
navigator.clipboard.writeText(text).then(function() {
  /* clipboard successfully set */
}, function() {
  /* clipboard write failed */
});

推荐阅读