Navigator:clipboard 属性
Navigator
接口的只读属性 clipboard
返回一个用于读写剪贴板内容的 Clipboard
对象。
这是 Clipboard API 的入口点,可用于在 Web 应用程序中实现剪切、复制和粘贴功能。
值
用于访问系统剪贴板的 Clipboard
对象。
示例
以下代码使用 navigator.clipboard
来访问系统剪贴板,以便读取剪贴板中的文本内容。
js
navigator.clipboard
.readText()
.then(
(clipText) => (document.querySelector(".cliptext").innerText = clipText),
);
该代码片段将设置了 "cliptext"
类的元素的内容替换为剪贴板中的文本内容。这段代码可能用于浏览器扩展程序,该扩展程序显示当前剪贴板的内容,并会定期或在特定事件触发时自动更新。
如果剪贴板为空或不包含文本,则清除 "cliptext"
元素的内容。这是因为如果剪贴板为空或不包含文本,readText()
会返回空字符串。
规范
Specification |
---|
Clipboard API and events # navigator-clipboard |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
clipboard |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.