简体   繁体   English

tinymce编辑器getContent在Wordpress中为null

[英]tinymce editor getContent null in Wordpress

I am trying to use the getContent feature of the Tinymce editor in WordPress admin area but to no avail. 我正在尝试在WordPress管理区域中使用Tinymce编辑器的getContent功能,但无济于事。

Here is a working pen of what I want to do 这是我想做的工作笔

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

HTML 的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 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>");
}
 });

Why am I getting the null reference error in Wordpress if it works in codepen. 如果在Codepen中可以正常工作,为什么在Wordpress中会出现空引用错误。

The issue here is likely that the init() method is asynchronous. 这里的问题可能是init()方法是异步的。 You appear to be trying to use the tinymce object (eg tinymce.activeEditor ) immediately after calling init() but the initialization process is likely not complete yet so you have no "active" editor on the page. 您似乎在调用init()之后立即尝试使用tinymce对象(例如tinymce.activeEditor ),但是初始化过程可能尚未完成,因此页面上没有“活动”编辑器。

If you want to interact with the editor "as soon as its available" you can use an onInit trigger in your TinyMCE init() call. 如果要“尽快”与编辑器进行交互,可以在TinyMCE init()调用中使用onInit触发器。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM