簡體   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