I have a contentEditable div
which in some cases gets display: none
, then again display: block
(inline-block
), I use div.focus()
when display
is set to block
, but cursor is at the beggining of开发者_如何转开发 the text and I can't change its position to end on Chrome.
How can I make it to set cursor at the end, when the div gets focus?
This uses jQuery but can be generalised to normal JavaScript. The important part is:
var range = document.createRange(),
selection = window.getSelection();
range.setStartAfter(div.lastChild); // set cursor
selection.removeAllRanges();
selection.addRange(range); // apply cursor position
http://jsfiddle.net/te93D/1/
精彩评论