2011-10-05 95 views
6

可能重複:
CKEditor - Set cursor position to end of text設置插入符號的焦點後的CKEditor最終

我有很多的內容<div>。點擊這個div之後,CKEditor被加載來編輯這個div。

現在我想在用編輯器替換內容後將插入符/光標設置到內容的末尾。

我的代碼目前是這樣的:

var editor = CKEDITOR.replace('content', { 
    // Settings 

    // Event listeners 
    on: { 
    instanceReady: function(evt) { 
     var editor = evt.editor; 

     // give focus (displays caret at the beginning of the content, not the end) 
     editor.focus(); 
    } 
    } 
}); 

回答

4

有點擺弄之後,我知道了用下面的代碼工作:

$(document).ready(function() { 

    CKEDITOR.on('instanceReady', function(ev) { 

     ev.editor.focus(); 

     var s = ev.editor.getSelection(); // getting selection 
     var selected_ranges = s.getRanges(); // getting ranges 
     var node = selected_ranges[0].startContainer; // selecting the starting node 
     var parents = node.getParents(true); 

     node = parents[parents.length - 2].getFirst(); 

     while (true) { 
      var x = node.getNext(); 
      if (x == null) { 
       break; 
      } 
      node = x; 
     } 

     s.selectElement(node); 
     selected_ranges = s.getRanges(); 
     selected_ranges[0].collapse(false); // false collapses the range to the end of the selected node, true before the node. 
     s.selectRanges(selected_ranges); // putting the current selection there 
    } 

}); 

的理念是:

  1. 獲取根節點(不是主體)
  2. Advance到下一個節點,直到沒有更多的節點前進到。
  3. 選擇最後一個節點。
  4. 收起它
  5. 設定範圍
+0

大......它的工作:)(忘了這個功能,我們決定離開這個功能了...) –

+0

我準備放棄以及,但我的老闆真的真的很想它:P –

+0

@ KeesC.Bakker希望你能幫助我...我通過以下在我的頁面底部啓動ckeditor: window.onload = function(){ CKEDITOR .replace( '消息'); }; 那麼我究竟在哪裏放置了上面的代碼來完成這項工作?我已經在我的HTML中的以下內容: <= $消息;?>而$消息如下:

Abela