admin管理员组文章数量:1323029
When the Summernote text editor initializes with the focus
property set to true
, the editor gains focus but places the cursor at the beginning of the editor.
My preference would be to have the cursor placed where the user had clicked initially. At a bare minimum I would just need to place the cursor at the end of the editor.
The Summernote API doesn't seem to directly support this and I've tried various hacky looking solutions; Such as emptying the text area, creating the editor, then inserting the HTML nodes. The cursor remains at the beginning of the line still.
Forgot to include my fiddle: /
When the Summernote text editor initializes with the focus
property set to true
, the editor gains focus but places the cursor at the beginning of the editor.
My preference would be to have the cursor placed where the user had clicked initially. At a bare minimum I would just need to place the cursor at the end of the editor.
The Summernote API doesn't seem to directly support this and I've tried various hacky looking solutions; Such as emptying the text area, creating the editor, then inserting the HTML nodes. The cursor remains at the beginning of the line still.
Forgot to include my fiddle: http://jsfiddle/madChemist/gvy3po4L/1/
Share Improve this question edited Feb 2, 2016 at 18:24 Mad-Chemist asked Feb 2, 2016 at 18:10 Mad-ChemistMad-Chemist 4871 gold badge6 silver badges18 bronze badges1 Answer
Reset to default 6Here is the solution I've modified to work for me:
$.fn.extend({
placeCursorAtEnd: function() {
// Places the cursor at the end of a contenteditable container (should also work for textarea / input)
if (this.length === 0) {
throw new Error("Cannot manipulate an element if there is no element!");
}
var el = this[0];
var range = document.createRange();
var sel = window.getSelection();
var childLength = el.childNodes.length;
if (childLength > 0) {
var lastNode = el.childNodes[childLength - 1];
var lastNodeChildren = lastNode.childNodes.length;
range.setStart(lastNode, lastNodeChildren);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
return this;
}
});
Which originally came from this post: https://stackoverflow./a/6249440/2921200 and then I modified to work with jQuery & specifically against Summernote.
本文标签: javascriptSummernoteOn focusinsert cursor at the end of editorStack Overflow
版权声明:本文标题:javascript - Summernote - On focus, insert cursor at the end of editor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742110819a2421247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论