ClipboardItem:getType() 方法
getType() 是 ClipboardItem 接口的一个方法。它返回一个 Promise,该 Promise 在解决时会提供请求的 MIME 类型对应的 Blob 对象,如果未找到该 MIME 类型则会返回错误。
语法
js
getType(type)
参数
返回值
异常
NotFoundErrorDOMException-
type与已知的 MIME 类型不匹配。 TypeError-
未指定参数,或
type不属于ClipboardItem的类型。
示例
在以下示例中,我们使用 clipboard.read() 方法返回剪贴板上的所有项目。对于每个项目,我们将 ClipboardItem.types 属性传递给 getType() 方法,该方法返回相应的 Blob 对象。
js
async function getClipboardContents() {
try {
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
const blob = await clipboardItem.getType(type);
// we can now use blob here
}
}
} catch (err) {
console.error(err.name, err.message);
}
}
规范
| 规范 |
|---|
| Clipboard API 和事件 # dom-clipboarditem-gettype |
浏览器兼容性
加载中…