如何在Javascript中实现剪切、复制和粘贴功能

How to Implement Cut,Copy and Paste functions in Javascript

本文关键字:复制 功能 Javascript 实现      更新时间:2023-09-26

我有一个可视化树,在其中我必须应用剪切、复制和粘贴功能来剪切顶点、复制顶点并粘贴它。我希望代码能在IE中工作。有人能帮我在java脚本中剪切、复制和粘贴代码吗。

提前感谢

在剪贴板上阅读更多API和事件

document.addEventListener('beforecopy', function(e){
    if(weHaveDataToCopy()){ // use your web app's internal logic to determine if something can be copied
        e.preventDefault(); // enable copy UI and events
    }
});
document.addEventListener('copy', function(e){
    e.clipboardData.setData('text/plain', 'Hello, world!');
    e.clipboardData.setData('text/html', '<b>Hello, world!</b>');
    e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
});
document.addEventListener('paste', function(e){
    if(e.clipboardData.types.indexOf('text/html') > -1){
        processDataFromClipboard(e.clipboardData.getData('text/html'));
        e.preventDefault(); // We are already handling the data from the clipboard, we do not want it inserted into the document
    }
});

读取良好的