2012-07-19 180 views
1

我想在特定位置自動添加內容到TinyMce編輯器。 對此,我使用以下示例:TinyMce ed.selection.select在Internet Explorer中引發錯誤

首先應該做的是在要創建的內容末尾添加一個範圍。 然後一旦插入,這樣做...

ed.selection.select('span#caret_pos_holder')[0]); //選擇範圍 ed.dom.remove(ed.dom.select('span#caret_pos_holder')[0]); //刪除跨度

這裏實測值: What's the best way to set cursor/caret position?

這適用於去鉻細但拋出在IE中的錯誤

「DOM異常:INDEX_SIZE_ERR(1)」

+0

+1第一個問題 – Thariama 2012-07-19 13:00:53

回答

1

這裏是功能(位於自己的自定義插件之一)我用

// sets the caret position 
    // ed is the editor instance 
    // element is the html element located in the editor 
    // start defines if the caret should get set to the start of the element (otherwise the end) 
    setCursor: function (ed, element, start) { 

     var doc = ed.getDoc(); 
     var edwin = ed.getWin(); 
     if (typeof doc.createRange != "undefined") { 
      var range = ed.selection.dom.createRng(); 
      range.selectNodeContents(element); 
      range.collapse(start); 
      var win = doc.defaultView || doc.parentWindow; 
      var sel = tinymce.isIE ? doc.selection : edwin.getSelection(); 
      sel.removeAllRanges(); 
      sel.addRange(range); 
     } else if (typeof doc.body.createTextRange != "undefined") { 
      var textRange = doc.body.createTextRange(); 
      textRange.moveToElementText(element); 
      textRange.collapse(start); 
      textRange.select(); 
     } 
    }, 

調用示例:

setCursor(ed, $(ed.getBody()).find('span#caret_pos_holder').get(0), 0); 
+0

感謝您的回答,這在鉻中工作,但在IE中,我得到一個錯誤,該屬性或方法removeAllRanges不支持此對象。 – Jeroen 2012-07-19 09:04:23

+0

只有在ie9這是一個問題,它在ie 7/8 – Jeroen 2012-07-19 09:08:08

+0

罰款,你可以嘗試評論「removeAllRanges」出來,並再次檢查在IE9 – Thariama 2012-07-19 10:13:48