2010-12-19 95 views
1

考慮下面的HTML:TinyMCE的設置重點文本部分

<div id="block-container"> 
    <div id="some-background"></div> 
    <div id="text-div">Focus should be here when this HTML goes into the editor</div> 
</div> 

我想插入符號是在文本格 - 更確切地說在第一個文本元素 - 當它在TinyMCE的編輯器中打開。

可能有一種方法可以將類似「.default-focused」的類添加到此類元素,並根據類設置焦點。有沒有其他(普遍)的方式來實現這一目標?

爲什麼我不能使用「默認聚焦」方式: 1.考慮到我擁有的數據量和添加課程的類別,添加課程可能是一項艱鉅的任務。2.更重要的是,用戶可以更改該HTML並可以刪除該類。

回答

1

好吧,如果你知道在哪個元素插入符將被放置你可以使用這個功能的短

// 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(); 
    } 
}, 
+0

謝謝。任何其他解決方案,當我不知道應該設置焦點的元素? – Saim 2010-12-21 14:43:25

+0

不,但請列出您無法獲取元素的場景? – Thariama 2010-12-21 14:59:50