How to get the element in front of the cursor using div, with contenteditable as true

when using div with conteneditable = true, you need to get the element in front of the cursor when you encounter the cursor problem. Does anyone know the solution?

May.22,2021

window.getSelection () find out? And then find the parents one floor at a time?

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

Menu