鍍金池/ 問答/網絡安全  HTML/ 使用contenteditable為true的div,如何獲取光標前的元素

使用contenteditable為true的div,如何獲取光標前的元素

在使用conteneditable = true的div時,遇到的光標問題,需要獲取到光標前元素,有人知道解決辦法么

回答
編輯回答
柒槿年

window.getSelection()了解一下?然后一層一層找父級?

var interval;
clearInterval(interval);
interval = setInterval(()=>{
    console.log(window.getSelection(),window.getSelection().anchorNode.textContent.substring(0,window.getSelection().anchorOffset))
},500)

2017年5月23日 16:22