2010-12-17 180 views
3

我想寫一個tinyMCE插件,允許嵌套sub和sup標籤。TinyMCE移動光標

現在我寫了一個命令,當你按下按鈕/擊中快捷鍵時,在光標處插入原始HTML''。但是,只有當我可以將光標移動到新標籤的中間時,這纔會起作用。

我似乎無法找到任何有關如何移動光標的文檔。有一些建議的黑客,但他們很hacky。這並不難,因爲據我瞭解,這是[b][i]按鈕的工作原理。

有沒有更好的方法來做到這一點?我怎樣才能編寫一個tinyMCE函數,讓用戶進入「子模式」或「sup模式」,並允許它們嵌套「sub」和「sup」模式?

謝謝!

回答

3

該函數將光標設置爲指定的html元素。

// sets the cursor to the specified element, ed ist the editor instance 
// start defines if the cursor is to be set at the start or at the end 
setCursor: function (ed, element, start) { 

    var doc = ed.getDoc(); 
    if (typeof doc.createRange != "undefined") { 
     var range = doc.createRange(); 
     range.selectNodeContents(element); 
     range.collapse(start); 
     var win = doc.defaultView || doc.parentWindow; 
     var sel = win.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(); 
    } 
}, 

爲了嵌套sups,你需要確保sups和subs可以嵌套。請檢查extended_valid_elementsvalid_elements配置參數。 sub和sup可能不會被默認嵌套!您需要覆蓋規則集的這部分內容。

+0

請幫忙。此代碼在Opera(11.10,2092,Win32XP)中不起作用:未捕獲的異常:[object DOMException] ... range.selectNodeContents(el); – 2011-04-25 13:48:47

+0

我擔心歌劇不完全支持Tinymce。您可能想看看最新的tinymce beta源代碼 - 內置函數將設置光標 - 也許這將適用於您的歌劇用例 – Thariama 2011-04-26 08:35:53

+0

我不知道發生了什麼,但添加了幾行代碼,一切都變得好:) – 2011-04-26 14:40:10