您可以通過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上的示例。