舉個(gè)例子,想要實(shí)現(xiàn)這種效果:里面有200個(gè)字,我用鼠標(biāo)選中第20-36個(gè)字,然后把它拖放到第60字的位置。這個(gè)功能在notepad2.exe中早就實(shí)現(xiàn)了,而我希望在textarea中實(shí)現(xiàn)它。我這么寫:document.addEventListener("dragover",function(e){e.stopPropagation();e.preventDefault();},false);Editor.addEventListener("dragover",function(e){e.stopPropagation();e.preventDefault();//該如何在鼠標(biāo)拖動(dòng)過(guò)程中定位文本插入點(diǎn)呢?},false);//Editor引用那個(gè)Editor.addEventListener("dragstart",function(e){console.log(e.type);varstartPos=Editor.selectionStart,endPos=Editor.selectionEnd;vartmpStr=Editor.value;varword=tmpStr.substring(startPos,endPos);//console.log(word);取得選中的文本},false);Editor.addEventListener("drop",function(e){e.stopPropagation();e.preventDefault();if(e.dataTransfer.types=='text/plain'){//現(xiàn)在問(wèn)題在這里了,drop的時(shí)候無(wú)法取得鼠標(biāo)放開(kāi)時(shí)在文本中的位置(即插入點(diǎn)位置)。怎么辦呢?}},false);如果這能實(shí)現(xiàn),我還希望實(shí)現(xiàn):按住Ctrl的情況下拖拽文本,實(shí)現(xiàn)復(fù)制文本到另一個(gè)位置的功能。
怎樣用js對(duì)一個(gè)textarea中的文本實(shí)現(xiàn)拖拽呢?
郎朗坤
2019-04-19 16:29:26