如何使用JavaScript获取Tinymce textarea的内容

poo*_*oja 44 javascript tinymce

我有一系列的内容,然后我们如何在javascript中获取Tinymce textarea的内容

小智 69

我用代码解决了它:

// Get the HTML contents of the currently active editor
tinyMCE.activeEditor.getContent();

// Get the raw contents of the currently active editor
tinyMCE.activeEditor.getContent({format : 'raw'});

// Get content of a specific editor:
tinyMCE.get('content id').getContent()
Run Code Online (Sandbox Code Playgroud)

activeEditor是当前编辑器,但我使用tinyMCE.get('editor1').getContent()无法获取我的编辑器的值,希望它可以帮助你

我的主页是机器鸟

Tinymce API:http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent

  • 如果版本> = 4.5,则使用`tinymce`代替`tinyMCE` (4认同)
  • `{format : 'raw'}` 解决了例如未从编辑器获取彩色文本的问题。 (2认同)

z.e*_*yyo 30

让我们说你的mce textarea实例是:

<textarea id="editor1" ....></textarea>
Run Code Online (Sandbox Code Playgroud)

然后你得到如下内容:

var content =  tinyMCE.getContent('editor1');
Run Code Online (Sandbox Code Playgroud)

如果你的意思是你在一个页面上有多个mce编辑器实例,并且你想获得内容,那么尝试这种方法:

var inst, contents = new Object();
for (inst in tinyMCE.editors) {
    if (tinyMCE.editors[inst].getContent)
        contents[inst] = tinyMCE.editors[inst].getContent();
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将每个编辑器内容添加到一个数组中

  • 这个答案最有可能是旧版本的TinyMCE.要使用最新的(4.x)执行此操作,@ jqpress的答案是正确的. (4认同)

小智 18

我有同样的问题.我已经解决了使用此代码:

tinyMCE.get('editor1').getContent();
Run Code Online (Sandbox Code Playgroud)

来源:spocke是作者


Tha*_*ama 15

你可以使用:

tinymce.get(editorid).getContent();
Run Code Online (Sandbox Code Playgroud)


小智 5

就我而言(v4.3.12),以上方法都不起作用,所以我做了一个解决方法:

网页代码:

<div id="wrapper">
    <textarea id="editable_container" name="editable_container"></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery 代码:

var iframe = $('#editable_container_ifr');
var editorContent = $('#tinymce[data-id="editable_container"]', iframe.contents()).html();
console.log(editorContent);
Run Code Online (Sandbox Code Playgroud)

我的tinyMCE编辑器的占位符文本区域在哪里editable_container,可编辑区域的iframe id是通过_ifr向占位符的id添加后缀生成的,并且content-editable容器(包含格式化文本)有一个tinymce带有data-id占位符id属性的id。