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

时间:2015-04-02 03:41:12

标签: javascript

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

先谢谢

1 个答案:

答案 0 :(得分:2)

详细了解Clipboard API and events

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
    }
});

good reading