javascript - 单击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");
解决方案
使用剪贴板 API:
const text = window.localStorage.getItem('content');
navigator.clipboard.writeText(text).then(function() {
/* clipboard successfully set */
}, function() {
/* clipboard write failed */
});
推荐阅读
- java - Selenium WebDriver (java) 可以与浏览器的检查工具元素选择器交互吗?
- reactjs - 组件声明 te 被渲染;函数与变量的返回值
- c# - 我如何安全地处理变量?
- algorithm - 显示不相交哈密顿路径的 np 完备性
- ag-grid - ag-grid 如何设置浮动过滤器高度
- r - 模拟 for 循环并将值保存在向量中
- r - 将ggplot轴文本链接到rmarkdown中的不同页面
- angular - 在Angular中使用组件库导出节点模块?
- geolocation - 多个圆圈上的传单地理围栏......检测内部/外部标记
- r - facet_grid 不同的面板尺寸