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.