admin管理员组文章数量:1328953
I have a contentEditable
div.
Let's say the user clicks a button that inserts HTML into the editable area.
So, they click a button and the following is added to the innerHTML
of the contentEditable
div:
<div id="outside"><div id="inside"></div></div>
How do I automatically place the cursor (ie caret) IN the "inside" div? Worse, how can this work in IE and FF?
I have a contentEditable
div.
Let's say the user clicks a button that inserts HTML into the editable area.
So, they click a button and the following is added to the innerHTML
of the contentEditable
div:
<div id="outside"><div id="inside"></div></div>
How do I automatically place the cursor (ie caret) IN the "inside" div? Worse, how can this work in IE and FF?
Share Improve this question edited Mar 16, 2022 at 16:43 General Grievance 5,03338 gold badges37 silver badges56 bronze badges asked Dec 12, 2009 at 9:26 Ben McBen Mc 2,0786 gold badges30 silver badges37 bronze badges 03 Answers
Reset to default 7For IE:
var range= document.body.createTextRange();
range.moveToElementText(document.getElementById('inside'));
range.select();
For Mozilla:
var range= document.createRange();
range.selectNodeContents(document.getElementById('inside'));
var selection= window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
In theory the Mozilla version should also work in Webkit and Opera. In practice Webkit selects nothing and Opera selects the whole page. Sigh. This is still not well-supported territory.
As far as i could understand from your question:
If it is contentEditable editable/typeable, you may try this:
// you insert content with your code and after that
document.getElementById('contentEditable_id_here').focus();
FYI focus() doesn't work for non-form elements with contentEditable enabled in Google Chrome (I just tried it on a <li>
inside a list that is editable and nothing happened in Chrome 10.0.648.82 beta on Win XP Pro SP3).
本文标签: contentEditable javascript caret placement in divStack Overflow
版权声明:本文标题:contentEditable javascript caret placement in div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742256705a2441759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论