开发者

contentEditable div display: none / block focus problem on Chrome

开发者 https://www.devze.com 2023-04-02 13:38 出处:网络
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开发者_如

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

精彩评论

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