【问题标题】:tinymce editor getContent null in WordpressWordPress中的tinymce编辑器getContent null
【发布时间】:2017-04-20 13:35:22
【问题描述】:

我正在尝试在 WordPress 管理区域中使用 Tinymce 编辑器的 getContent 功能,但无济于事。

这是我想做的工作笔

https://codepen.io/anon/pen/oWxjyM?editors=1111

HTML

<textarea class="wp-editor-area" rows="20" autocomplete="off" cols="40" name="wprss_ftp_post_prepend" id="wprsspostprepend">&lt;p&gt;To give is good&lt;/p&gt;</textarea>

JS

jQuery(document).ready(function() {
  $("#wprsspostprepend").addClass("prepend_editor");
var name = "wprsspostprepend";
if($("#" + name).length > 0) {

  tinyMCE.init({
        mode : "specific_textareas",
        editor_selector : "prepend_editor"  
});
   var content =  tinyMCE.activeEditor.getContent();
  alert(content);
    tinyMCE.activeEditor.setContent("<div id='random-wrap'>" + content + "</div>");
}
 });

如果它在 codepen 中工作,为什么我在 Wordpress 中得到空引用错误。

【问题讨论】:

    标签: javascript jquery wordpress tinymce


    【解决方案1】:

    这里的问题很可能是init() 方法是异步的。您似乎在调用 init() 后立即尝试使用 tinymce 对象(例如 tinymce.activeEditor),但初始化过程可能尚未完成,因此您在页面上没有“活动”编辑器。

    如果您想“尽快”与编辑器进行交互,您可以在 TinyMCE init() 调用中使用 onInit 触发器。例如:

    tinymce.init({
      selector: '#myTextArea",
      setup: function (editor) {
        editor.on('init', function () {
          this.setContent('<p>Add content via on init!</p>');
        });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2021-06-08
      • 2014-07-27
      • 1970-01-01
      相关资源
      最近更新 更多