开发者

TinyMCE Setting focus in text part

Consider the following HTML:

<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>

I want the caret be in the text-div -- more precisely in the first text element -- when it opens in the TinyMCE editor.

There could be a way to add some class like ".default-focused" to such element and set focus based on the class. Is there any other (generalized) way to achieve this?

The reaso开发者_开发技巧n why I can't go with the ".default-focused" way: 1. It could be huge task to add class considering the amount of data I have and 2. More importantly, user can change the HTML and can remove the class.


Well, if you know in which element the caret is to be placed you may use this short function

// 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜