2016-06-14 4 views
0

kopieren Ich habe folgende JS-Code in die Zwischenablage zu kopieren:JS Copy discontiguous Abschnitt

function copyAll(copyEl){ 
    var textToCopy = $(copyEl)[0]; 

    var range = document.createRange(); 
    range.selectNode(textToCopy); 
    window.getSelection().addRange(range); 


    try { 
     // Now that text is selected, execute the copy command 
     var copyRet = document.execCommand('copy'); 
     var msg = copyRet ? 'successful' : 'unsuccessful'; 
     $('#copyResult').stop(true, true).fadeOut(0).html('Copied to clipboard').fadeIn(500).fadeOut(3000); 

     // Remove the selections 
     window.getSelection().removeAllRanges(); 

     console.log('Copy command was ' + msg); 
    } 
    catch(err) { 
     $('#copyResult').stop(true, true).fadeOut(0).html('Oops, unable to copy').fadeIn(500).fadeOut(3000); 
     console.log('Oops, unable to copy'); 
    } 
} 

Wenn diese Funktion ausgeführt wird, bekomme ich diesen Fehler und melden Sie sich an der Konsole:

Discontiguous selection is not supported. 
Copy command was successful 

an dieser Zeile:

window.getSelection().addRange(range); 

Und der Text wird nicht kopiert.

Also, wie bekomme ich einen Fehler, immer noch bekomme ich Copy command was successful?

Auch dieses Verhalten wird nicht immer beobachtet. Manchmal bekomme ich diesen Fehler nicht, und manchmal bekomme ich diesen Fehler, aber der Text wird trotzdem in die Zwischenablage kopiert.

Ich arbeite nur an Chrome.

Antwort

0

Sieht aus, als wäre es ein Fehler auf der Chrome-Seite. Überprüfen Sie, ob es jetzt richtig funktioniert. Mehr Details here.

0

Der Grund, dass dieser Fehler ausgelöst wird, liegt daran, dass rangeCount des Auswahlobjekts nicht null ist.

Da @dropout die Chrome bug erwähnt hat, wird addRange zum Selection-Objekt vermieden, wenn es bereits einen Auswahlbereich hat.

Und die Nachricht 'Befehl wurde erfolgreich gesendet' wird empfangen, weil der Auswahlbereich nicht null ist. Was auch immer ausgewählt wurde, wurde hinzugefügt.

Um dies zu beheben, sollten Sie nach dem RangeCount suchen. Und wenn rangeCount nicht 0 ist, können Sie window.getSelection().empty() oder window.getSelection().removeAllRanges() auslösen, dann nur addRange und gehen Sie für den copy Befehl.

function copyAll(copyEl){ 
    var textToCopy = $(copyEl)[0]; 

    var range = document.createRange(); 

    if(range.rangeCount > 0){ 
    range.removeAllRanges(); 
    } 
    range.selectNode(textToCopy); 
    window.getSelection().addRange(range); 


    try { 
    // Now that text is selected, execute the copy command 
    var copyRet = document.execCommand('copy'); 
    var msg = copyRet ? 'successful' : 'unsuccessful'; 
    $('#copyResult').stop(true, true).fadeOut(0).html('Copied to clipboard').fadeIn(500).fadeOut(3000); 

    // Remove the selections 
    window.getSelection().removeAllRanges(); 

    console.log('Copy command was ' + msg); 
    } 
    catch(err) { 
     $('#copyResult').stop(true, true).fadeOut(0).html('Oops, unable to copy').fadeIn(500).fadeOut(3000); 
     console.log('Oops, unable to copy'); 
    } 

}