js 图片复制到剪贴板 火狐
时间: 2023-07-12 15:08:04 浏览: 168
在火狐浏览器中,可以使用 Clipboard API 来实现将图片复制到剪贴板的功能。以下是一个简单的示例代码:
```javascript
// 获取要复制的图片
var img = document.getElementById('myImage');
// 创建一个虚拟的 input 元素
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('value', img.src);
document.body.appendChild(input);
// 选中 input 元素中的内容
input.select();
// 使用 Clipboard API 将内容复制到剪贴板
navigator.clipboard.write([
new ClipboardItem({
[img.type]: img.blob,
}),
]).then(function() {
console.log('Image copied to clipboard.');
}).catch(function(error) {
console.error('Failed to copy image: ', error);
});
// 移除虚拟的 input 元素
document.body.removeChild(input);
```
在上面的代码中,我们先获取了一个图片元素,然后创建了一个虚拟的 input 元素,并将图片的 URL 设置为 input 元素的值。接着,我们选中 input 元素中的内容,并使用 Clipboard API 将图片复制到剪贴板。最后,我们将虚拟的 input 元素从页面中移除。
阅读全文
相关推荐















