繁体   English   中英

Summernote - 在焦点上,在编辑器的末尾插入光标

[英]Summernote - On focus, insert cursor at the end of editor

当 Summernote 文本编辑器初始化时focus属性设置为true ,编辑器获得焦点但将光标置于编辑器的开头。

我的偏好是将光标放在用户最初单击的位置。 至少我只需要将光标放在编辑器的末尾。

该Summernote API似乎并没有直接支持这样做,我已经尝试了各种哈克寻找解决方案; 例如清空文本区域,创建编辑器,然后插入 HTML 节点。 光标仍然停留在行首。

忘了包括我的小提琴: http : //jsfiddle.net/madChemist/gvy3po4L/1/

这是我修改后适合我的解决方案:

$.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;
    }
});

最初来自这篇文章: https ://stackoverflow.com/a/6249440/2921200 然后我修改为使用 jQuery & 特别是针对 Summernote。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM