关于div可编辑的复制粘贴问题
1 todoFilter(e) { 2 e.preventDefault(); 3 if (!e.target.getAttribute('contenteditable')) { 4 return; 5 } 6 const cpTxt = e 7 .clipboardData 8 .getData('text'); 9 10 const selection = window.getSelection ? window.getSelection() : document.selection; 11 if (!selection.rangeCount) return; 12 const range = selection.createRange ? selection.createRange() : selection.getRangeAt(0); 13 window._range = range; 14 if (!window.getSelection) { 15 range.pasteHTML(cpTxt); 16 range.collapse(false); 17 range.select(); 18 } else { 19 range.collapse(false); 20 const hasR = range.createContextualFragment(cpTxt); 21 let hasR_lastChild = hasR.lastChild; 22 while (hasR_lastChild && hasR_lastChild.nodeName.toLowerCase() == 'br' && hasR_lastChild.previousSibling && hasR_lastChild.previousSibling.nodeName.toLowerCase() == 'br') { 23 var e = hasR_lastChild; 24 hasR_lastChild = hasR_lastChild.previousSibling; 25 hasR.removeChild(e); 26 } 27 range.insertNode(hasR); 28 if (hasR_lastChild) { 29 range.setEndAfter(hasR_lastChild); 30 range.setStartAfter(hasR_lastChild); 31 } 32 selection.removeAllRanges(); 33 selection.addRange(range); 34 } 35 },