javascript - 复制js后如何返回选择?
问题描述
文字复制功能:
function copyToClipboard(str) {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
哪个更好:扫描整个文档并查找设置了焦点的元素,或者将选择器传递给函数的第二个参数,您希望将选择返回到该函数,然后再次设置焦点?
解决方案
推荐阅读
- ios - How to open local HTML files in iPhone browsers?
- python - 当程序的一部分在 Python 中等待时,如何做其他事情?
- javascript - 如何在反应中将功能和对象传输到功能组件
- probability - Is there a better way to program this,
- html - Display bug for email on Outlook (Microsoft 365) in a table cell
- javascript - 如何将属性函数添加到 Java 中另一个类的对象?
- android - Android App 中的异常:Lorg/jacoco/agent/rt/internal_8ff85ea/Offline 的解析失败;
- vue.js - 我可以在 Phaser Scene 函数(即创建或更新)中访问 Vue.js 选项 API 方法吗?
- python - sh 文件中 exec "$@" 的问题
- amazon-web-services - 通过 CDK 为 AWS SES(简单电子邮件服务)配置 AWS Cognito:服务相关角色权限 - 如何执行此操作?