【问题标题】:TinyMCE Setting focus in text partTinyMCE 在文本部分设置焦点
【发布时间】:2010-12-19 17:13:05
【问题描述】:

考虑以下 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>

当在 TinyMCE 编辑器中打开时,我希望插入符号位于 text-div 中——更准确地说是在第一个文本元素中。

可能有一种方法可以向此类元素添加诸如“.default-focused”之类的类,并根据该类设置焦点。有没有其他(通用)方法来实现这一点?

我不能采用“.default-focused”方式的原因: 1. 考虑到我拥有的数据量,添加类可能是一项艰巨的任务 2. 更重要的是,用户可以更改 HTML 并删除类。

【问题讨论】:

    标签: text tinymce


    【解决方案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();
        }
    },
    

    【讨论】:

    • 谢谢。当我不知道应该设置焦点的元素时,还有其他解决方案吗?
    • 否,但请说出您无法获取该元素的场景?
    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 2011-10-31
    • 2010-10-10
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多