开发者

contentEditable div display: none / block focus problem on Chrome

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜