How do you set the focus of a TinyMCE textarea element?

后端 未结 13 1995
慢半拍i
慢半拍i 2020-12-29 20:33

I\'m using TinyMCE for a textarea on a page but it doesn\'t play nice in the tabbing order of the other elements.

I can use the following code to capture when I tab

相关标签:
13条回答
  • 2020-12-29 21:07

    Looks like for TinyMCE 4 a few of these solutions no longer work.

    // Doesn't seem to work on TinyMCE 4

    tinymce.execCommand('mceFocus',false,'id_of_textarea'); 
    

    // Didn't work

    $("id_of_textarea").tinymce().focus();
    
    tinyMCE.activeEditor.focus();
    

    What works for me

    // Works great in Chrome, but not at all in Firefox

    tinyMCE.init({
        //...
        auto_focus : "id_of_textarea"
    });
    

    // Add this so Firefox also works

    init_instance_callback : "customInitInstanceForTinyMce", // property to add into tinyMCE.init()
    
    function customInitInstanceForTinyMce() {
        setTimeout(function () {                         // you may not need the timeout
            tinyMCE.get('Description').focus();
        }, 500);
    }
    
    0 讨论(0)
提交回复
热议问题