Set focus on div contenteditable element

后端 未结 9 2097
深忆病人
深忆病人 2020-11-29 01:51

I have a

where I define by a WYSIWYG some elements. For example

,

, etc. I would l

9条回答
  •  旧巷少年郎
    2020-11-29 02:31

    You can try this code, it can auto focus in your last insert location.

    let p = document.getElementById('contenteditablediv')
    let s = window.getSelection()
    let r = document.createRange()
    r.setStart(p, p.childElementCount)
    r.setEnd(p, p.childElementCount)
    s.removeAllRanges()
    s.addRange(r)
    

提交回复
热议问题