如何使用 JavaScript 获取 Tinymce textarea 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4551834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the content of a Tinymce textarea with JavaScript
提问by pooja
i have an array of content then how we get content of Tinymce textarea in javascript
我有一个内容数组,然后我们如何在 javascript 中获取 Tinymce textarea 的内容
采纳答案by z.eljayyo
lets say your mce textarea instance is:
假设您的 mce textarea 实例是:
<textarea id="editor1" ....></textarea>
then you get the content as follows:
然后你得到的内容如下:
var content = tinyMCE.getContent('editor1');
if you mean you have multiple instances of mce editor on one page and you want to get content then try this approach:
如果您的意思是在一个页面上有多个 mce 编辑器实例并且您想获取内容,请尝试以下方法:
var inst, contents = new Object();
for (inst in tinyMCE.editors) {
if (tinyMCE.editors[inst].getContent)
contents[inst] = tinyMCE.editors[inst].getContent();
}
the above code adds each editor content into an array
上面的代码将每个编辑器内容添加到一个数组中
回答by jqpress
I solved it with code:
我用代码解决了它:
// 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()
the activeEditor is current editor,but i use tinyMCE.get('editor1').getContent() can not get the value of my editor, hope it can help you
activeEditor 是当前编辑器,但我使用 tinyMCE.get('editor1').getContent() 无法获取我的编辑器的值,希望它可以帮助您
Tinymce API: http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent
Tinymce API:http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent
回答by CyE
I had the same problem. I have solved using this code:
我有同样的问题。我已经使用此代码解决了:
tinyMCE.get('editor1').getContent();
Source: spocke is the author
来源:spocke 是作者
回答by Thariama
You may use:
您可以使用:
tinymce.get(editorid).getContent();
回答by Derenir
In my case (v4.3.12), none of the above worked, so I did a workaround:
在我的情况下(v4.3.12),以上都没有奏效,所以我做了一个解决方法:
Html code:
html代码:
<div id="wrapper">
<textarea id="editable_container" name="editable_container"></textarea>
</div>
JQuery code:
查询代码:
var iframe = $('#editable_container_ifr');
var editorContent = $('#tinymce[data-id="editable_container"]', iframe.contents()).html();
console.log(editorContent);
Where editable_container
is my tinyMCE editor's placeholder textarea, the editable area's iframe id is generated from adding a _ifr
postfix to the placeholder's id, and the content-editable
container (which contains the formatted text), has an id tinymce
with a data-id
attribute of the placeholder's id.
editable_container
我的 tinyMCE 编辑器的占位符 textarea在哪里,可编辑区域的 iframe id 是通过向_ifr
占位符 id添加后缀生成的,并且content-editable
容器(包含格式化文本)具有一个tinymce
带有data-id
占位符 id 属性的 id。
回答by Brennan James
tinymce.get('editorId').setContent(response.data);
回答by Mohamad Hamouday
This work for me for version 4 (9.8):
这对我来说适用于版本 4 (9.8):
var Content = tinyMCE.editors['Your_ID'].getContent();
回答by Ben Long
Use the getContent()
method from the TinyMCE API.
使用getContent()
TinyMCE API 中的方法。
Let's say you have initialized the editor on a textarea with id=”myTextarea”. First access the editor using that same id, then call getContent()
. For example:
假设您已经在 id="myTextarea" 的文本区域上初始化了编辑器。首先使用相同的 id 访问编辑器,然后调用getContent()
. 例如:
var myContent = tinymce.get('#myTextarea').getContent();
var myContent = tinymce.get('#myTextarea').getContent();
Or, instead of accessing the editor by id, you can access the active editor:
或者,您可以访问活动编辑器,而不是通过 id 访问编辑器:
var myContent = tinymce.activeEditor.getContent();
var myContent = tinymce.activeEditor.getContent();
If want to get the TinyMCE content without the HTML tags, you can pass in a parameter to indicate that you want the result in plaintext. For example:
如果想得到不带HTML标签的TinyMCE内容,可以传入一个参数,表示想要纯文本的结果。例如:
var myContent = tinymce.get('#myTextarea').getContent({format: 'text'});
var myContent = tinymce.get('#myTextarea').getContent({format: 'text'});
More info and examples here: https://www.tiny.cloud/blog/how-to-get-content-and-set-content-in-tinymce.
更多信息和示例在这里:https: //www.tiny.cloud/blog/how-to-get-content-and-set-content-in-tinymce。
回答by omid
tinymce.activeEditor.getContent();
回答by Phil McCarty
If you are more familiar with (and are using the jquery wrapper), you can also do this using this:
如果您更熟悉(并且正在使用 jquery 包装器),您还可以使用以下方法执行此操作:
$('#editor1').tinymce().getContent();
Where (editor1) is your selector.
其中 (editor1) 是您的选择器。