In JavaScript, it's possible to programmatically select text in an input
or textarea
element. You can focus an input with ipt.focus()
, and then select its contents with ipt.select()
. You can even select a specific range with ipt.setSelectionRange(from,to)
.
My question is: is there any way to do this in a contenteditable
element too?
I found that I can do elem.focus()
, to put the caret in a contenteditable
element, but subsequently running elem.select()
doesn't work (and nor does setSelectionRange
). I can't find anything on the web about it, but maybe I'm searching for the wrong thing...
By the way, if it makes any difference, I only need it to work in Google Chrome, as this is for a Chrome extension.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…