js设置/获取剪切板内容


//设置剪切板内容
document.addEventListener("copy",function () {
if (event.clipboardData || event.originalEvent) {
var clipboardData = (event.clipboardData || event.originalEvent.clipboardData);
const selection = "AAAAA";
clipboardData.setData('text/plain', selection.toString());
event.preventDefault();
}
});
注意:clipboardData.setData("参数1","参数2") 参数1的值要对应http的content-type的类型,如果没有设置参数1的话有可能会报错
//获取剪切板的内容
document.addEventListener("paste", function () {
if (event.clipboardData || event.originalEvent) {
var clipboardData = (event.clipboardData || window.clipboardData);
var val = clipboardData.getData('text');
console.log(val);
event.preventDefault();
}
});

可以参考这个:https://developer.mozilla.org/zh-CN/docs/Web/API/ClipboardEvent/clipboardData



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM