How to set the initial text in a TinyMCE textarea?

前端 未结 9 517
慢半拍i
慢半拍i 2021-01-02 07:31

I\'m in a curious situation where I previously had no problem achieving what I\'m looking for. The following code is a part of an HTML page which is to host a TinyMCE rich t

相关标签:
9条回答
  • 2021-01-02 07:59

    If you don't call tinyMCE's JavaScript function, you may have to deal with browser compatibility issues.

    If you only have one tinymce box on the page, you can just do this:

    tinyMCE.activeEditor.setContent('<span>some</span>');
    

    You can also set a format such as BBCode. Check out the setContent documentation.

    I would have your PHP code echo out the HTML into a JavaScript function and have it called with onload:

    function loadTinyMCE($html) {
        echo "
            <script type="text/javascript">function loadDefaultTinyMCEContent(){
                 tinyMCE.activeEditor.setContent('$html');
            }</script>
        ";
    }
    

    then

    <body onload="loadDefaultTinyMCEContent()">
    

    If you don't want to use the page onload event, tinymce has an init option called oninit that functions similarly.

    Alternatively, setupcontent_callback gives you direct access to the iframe.

    0 讨论(0)
  • 2021-01-02 08:01

    set in tinyMCE.init -

    init_instance_callback: function (editor) {
                    AFunction();
                }
    
    function AFunction(){
    //your code here
    tinyMCE.get('youtinytextareaname').setContent("your text here");
    }
    
    0 讨论(0)
  • 2021-01-02 08:02

    I've come to the same problem while using React, perhaps it's related to this. The initial value passed to the component is the original value. Once received, the component will take over and handle the content, regardless of whether a new value is passed to it. So if you are passing a string, then it's a constant and it exists at initial render of your component. But if you are passing a variable, it's possible that the initial value of the variable is different to the value you want to display (like it's probably null or undefined until you receive the value to display). In my case I receive an object with the page contents, one of which is the html to display as initial content. But the render method was first being fired with an empty object, and then with the actual object with data.

    0 讨论(0)
提交回复
热议问题