function getDivContent(wrapper){ var textareaText = ''; for (var i = 0; i < wrapper.childNodes.length; i++) { if (wrapper.childNodes[i].nodeName !== '#text') { if (wrapper.childNodes[i].innerHTML == '<br>') { textareaText += wrapper.childNodes[i].innerHTML } else { textareaText += wrapper.childNodes[i].innerText.replace(/\n$/,'') + ((i == wrapper.childNodes.length - 1|| wrapper.childNodes[i].innerText=='\n') ? '' : '<br>') } } else { textareaText += wrapper.childNodes[i].nodeValue + ((i == wrapper.childNodes.length - 1 || wrapper.childNodes[i+1].nodeName=='BR') ? '' : '<br>') } } return textareaText }
在光標(biāo)所在位置插入文本
if (!selection) { selection = getSelection(); } if (lastEditRange) { selection.removeAllRanges() selection.addRange(lastEditRange) // 判斷選定對(duì)象范圍是編輯框還是文本節(jié)點(diǎn) if (selection.anchorNode.nodeName != '#text') { if (selection.anchorNode == addLinkFrame.inputWrapper) { console.log(0) addLinkFrame.inputWrapper.insertBefore(html, addLinkFrame.inputWrapper.childNodes[selection.anchorOffset]) } else if (addLinkFrame.inputWrapper.childNodes.length == 0 || (addLinkFrame.inputWrapper.childNodes.length == 1 && !addLinkFrame.inputWrapper.childNodes[0].innerText)) { console.log(1) addLinkFrame.inputWrapper.innerHTML = ''; addLinkFrame.inputWrapper.appendChild(html) } else if (addLinkFrame.inputWrapper.childNodes.length > 0) { console.log(2) // 如果文本框的子元素大于0,則表示有其他元素,則按照位置插入表情節(jié)點(diǎn) for (var i = 0; i < addLinkFrame.inputWrapper.childNodes.length; i++) { if (addLinkFrame.inputWrapper.childNodes[i] == selection.anchorNode) { if (selection.anchorOffset == 0) { addLinkFrame.inputWrapper.childNodes[i].prepend(html) } } else { if (selection.anchorOffset == 0) { selection.anchorNode.prepend(html) } } } } // else { // console.log(2) // // 否則直接插入一個(gè)表情元素 // // $(addLinkFrame.inputWrapper).append(html) // addLinkFrame.inputWrapper.appendChild(html) // } // 創(chuàng)建新的光標(biāo)對(duì)象 var range = document.createRange(); // 光標(biāo)對(duì)象的范圍界定為新建的表情節(jié)點(diǎn) range.selectNodeContents(html) // 光標(biāo)位置定位在表情節(jié)點(diǎn)的最大長(zhǎng)度 range.setStart(html, html.length) // 使光標(biāo)開始和光標(biāo)結(jié)束重疊 range.collapse(true) // 清除選定對(duì)象的所有光標(biāo)對(duì)象 selection.removeAllRanges() // 插入新的光標(biāo)對(duì)象 selection.addRange(range) } else { console.log(3) // 如果是文本節(jié)點(diǎn)則先獲取光標(biāo)對(duì)象 var range = selection.getRangeAt(0) // 獲取光標(biāo)對(duì)象的范圍界定對(duì)象,一般就是textNode對(duì)象 var textNode = range.startContainer; // 獲取光標(biāo)位置 var rangeStartOffset = range.startOffset; // 文本節(jié)點(diǎn)在光標(biāo)位置處插入新的表情內(nèi)容 textNode.insertData(rangeStartOffset, html.innerText) // 光標(biāo)移動(dòng)到到原來(lái)的位置加上新內(nèi)容的長(zhǎng)度 range.setStart(textNode, rangeStartOffset + html.length) //光標(biāo)開始和光標(biāo)結(jié)束重疊 range.collapse(true) // 清除選定對(duì)象的所有光標(biāo)對(duì)象 selection.removeAllRanges() // 插入新的光標(biāo)對(duì)象 selection.addRange(range) } // 無(wú)論如何都要記錄最后光標(biāo)對(duì)象 lastEditRange = selection.getRangeAt(0) } else { console.log(4) // G.error('請(qǐng)選中你所需要添加光標(biāo)的位置'); var allhtml = $(addLinkFrame.inputWrapper).text() + html.innerText; $(addLinkFrame.inputWrapper).text(allhtml) }
聯(lián)系客服