TinyMCE:tinyMCE.init()尚未附加<textarea>

时间:2016-08-31 00:24:19

标签: javascript html tinymce-4

&lt; p&gt;所以,基本上,我想创建一个richtextbox,即使没有&lt; code&gt;&lt; textarea&gt;&lt; / code&gt;然而。我想自己附加richtextbox。不是&lt; strong&gt; tinyMCE&lt; / strong&gt;。问题是我认为在追加这个之后,我不会有任何tinyMCE事件。&lt; / p&gt; &lt; p&gt;我想到的就是......&lt; / p&gt; &LT p为H.;&LT;代码&GT; $( “&LT; TextArea&GT;&LT; / textarea的&gt;” 中)。tinyMCE的({选项})&LT; /代码&GT;这将返回richtextbox的html字符串。这样,我可以自己附加html字符串。请注意,它应该完美地工作,尤其是事件。&lt; / p&gt; &lt; p&gt;这甚至可能吗?&lt; / p&gt;

1 个答案:

答案 0 :(得分:0)

您可以通过javascript创建<textarea>,并在想要在屏幕上呈现编辑器之前将其附加到DOM。但是,您需要使用id作为textarea。

//an editor id needs to be used in this method
var editorId = "#myId";
var options = {
    //options

}
//Creates an editor instance and adds it to the EditorManager collection.
tinyMCE.createEditor(editorId, options);

//get the editor instance by its id 
var editor = tinyMCE.get(editorId);

//get the root element you want insert the editor into. (E.g. body element)
var root = document.getElementsByTagName('body')[0];

//create a textarea element
var textarea = document.createElement("textarea");
//set its id
textarea.setAttribute("id", editorId);
//append it to the root element
root.appendChild(textarea);

//register your events
registerEvents(editor);

//display editor on screen
editor.render();
//set content of editor
editor.setContent("<h1>Hello World!</h1><p>Hello World!</p>");

//retrieve content from editor
var htmlContent = editor.getContent();

console.log(htmlContent);

function registerEvents(editor) {
  editor.on("init", function(e) {
    console.log("Init", e);
  });

  editor.on("focus", function(e){
    console.log("Focus", e);
  });

  editor.on("blur", function(e){
    console.log("Blur", e);
  });
}

以下是JSFiddle上的示例。