We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在一些博客系统,如掘金的博客中,可以复制代码,它是如何实现的
The text was updated successfully, but these errors were encountered:
它一般可以使用第三方库 clipboard-copy 来实现,源码很简单,可以读一读
目前最为推荐的方式是使用 Clipboard API 进行实现
Clipboard API
navigator.clipboard.writeText(text)
而对于一些不支持 Clipboard API 的浏览器,使用以下 API 进行复制
Selection API
document.execCommand
选中主要利用了 Selection API 与 Range API
选中的代码如下
const selection = window.getSelection(); const range = document.createRange(); // RangeAPI: 制造区域 range.selectNodeContents(element); // Selection: 选中区域 selection.addRange(range); selectedText = selection.toString();
取消选中的代码如下
window.getSelection().removeAllRanges();
它有现成的第三方库可以使用: select.js
复制就比较简单了,execCommand
execCommand
document.execCommand('copy')
Sorry, something went wrong.
No branches or pull requests
在一些博客系统,如掘金的博客中,可以复制代码,它是如何实现的
The text was updated successfully, but these errors were encountered: