reactjs - 在 React 中使用 document.createRange 和 window.getSelection
问题描述
我正在使用 Reactjs。在 React中如何使用document.createRange
以及在 React 中如何使用?window.getSelection
document.execCommand('copy')
我想复制我的 html 表格的内容。所以我使用了代码
copyTable(e){
let elTable = this.tableElement.current.localName\\ elTable= "table"
this.copyData(elTable)
}
copyData(elToBeCopied){
let range, sel;
// Ensure that range and selection are supported by the browsers
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
// unselect any element in the page
sel.removeAllRanges();
try {
range.selectNodeContents(elToBeCopied);
sel.addRange(range);
} catch (e) {
range.selectNode(elToBeCopied);
sel.addRange(range);
}
document.execCommand('copy');
}
sel.removeAllRanges();
console.log('Element Copied! Paste it in a file')
}
我通过在我的 html 表中elTable
创建一个来获得价值。ref
调试时,调试器正在进入功能 copyData
。
但不能从表中复制数据。为什么?
我正在使用反应 js。
解决方案
推荐阅读
- c - 从特定地址初始化多维数组指针的正确方法
- python - 需要一个条件语句来填充基于字符串的列
- azure-devops - Azure DevOps 无法从其自己的存储库下载工件
- php - 如何在选择中显示分组项目?
- android - 将数字四舍五入到下一个最高的位置
- node.js - 默认 Firebase 应用不存在。确保在使用任何 Firebase 服务之前调用 initializeApp()。在 FirebaseAppError
- node.js - 无法通过 REST API 访问使用 Mongodb 部署的 Nodejs 应用程序
- php - 如何每 5 秒更新一次显示的用户的离线、在线状态?
- java - Java - poi:将一个 docx 附加到另一个 docx 时出现问题:使用未定义的命名空间 > 前缀:w15
- javascript - 达到 PM2 最大调用堆栈