18

我是 Javascript/TinyMCE 的初学者,我试图了解如何从编辑器中获取 HTML 内容,并使用简单的 alert() 函数显示它。

我的 HTML 页面上有这个极简配置:

<div id="tiny">
<script type="text/javascript">
tinyMCE.init({
        // General options
        mode : "specific_textareas",
        editor_selector : "mceEditor"
});
</script>
</div>

<form method="post" action="somepage">
        <textarea id="myarea1" class="mceEditor">This will be an editor.</textarea>
</form>

TinyMCE 网站上,他们解释说我必须使用这个:

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

这里也是_

tinymce.activeEditor.getContent()

我不知道为什么它不起作用

有人有想法吗?

4

4 回答 4

33

我不知道为什么它不起作用

它不起作用,因为

console.debug(tinyMCE.activeEditor.getContent());

tinymce.activeEditor.getContent();

这些语句没有被执行。

尝试跟随这个FIDDLE ....

tinyMCE.init({
        // General options
        mode : "specific_textareas",
        editor_selector : "mceEditor"
});

获取内容的功能......

function get_editor_content() {
  // Get the HTML contents of the currently active editor
  console.debug(tinyMCE.activeEditor.getContent());
  //method1 getting the content of the active editor
  alert(tinyMCE.activeEditor.getContent());
  //method2 getting the content by id of a particular textarea
  alert(tinyMCE.get('myarea1').getContent());
}

单击按钮获取编辑器的内容...

<button onclick="get_editor_content()">Get content</button> 
于 2012-12-24T16:56:18.317 回答
2

也许是这样?您的变量是,tinyMCE但您正在调用getContent()tinymceJS 区分大小写 ;)

于 2012-12-24T15:57:11.877 回答
1

我一直在寻找解决方案并尝试了上述一些方法,然后在 tinymce 文档中查找更多内容,发现这很有效。
使用微型 mce 4

function getHTML()
{
   tinymce.activeEditor.on('GetContent', function(e) {
     console.log(e.content);
   });
}

只需通过单击调用该函数并查看结果是什么...
我的来源是: http ://www.tinymce.com/wiki.php/api4:class.tinymce.ContentEvent

于 2014-08-23T18:43:40.470 回答
1

TinyMCE 创建一个格式为 '#textareaid'+'_ifr' 的 iframe 所以通过使用 jquery 我们可以查询到你喜欢的文本区域的 HTML 内容

iframe id 将是您的 textarea Id,并附加了“_ifr”。所以你可以从 tinyMce 中提取 HTML 内容

$('#textareaId_ifr').contents().find("html").html();
于 2017-11-07T04:05:31.100 回答