2015-04-02 123 views

回答

1

瞭解更多關於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

相關問題