2010-12-19 3 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> 

を私はキャレットがテキストのdivになりたい - より正確には最初のテキスト要素で - それはTinyMCEのエディタで開いたとき。

このような要素に ".default-focused"のようなクラスを追加し、そのクラスに基づいてフォーカスを設定する方法があります。これを達成するための他の(一般化された)方法はありますか?

私は「.DEFAULTに焦点を当てた」道を行くことができない理由:さらに重要なことは、ユーザーが変更することができます。2.私が持っているデータと の量を考慮したクラスを追加するには膨大な作業になる可能性があり 1 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

+0

いいえ、要素を取得できないシナリオに名前を付けますか? – Thariama

関連する問題