最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tinymce 4.2.0 got Cannot read property 'setContent' of null - Stack Overflow

programmeradmin1浏览0评论

I got Cannot read property 'setContent' of null, when i want to using setContent function. Intended for set value in text editor generated by Tinymce library. Is am wrong to implemented it?Below is my snippet code:

<textarea name="content"></textarea>      
<script src="assets/tinymce/tinymce.min.js"></script>
<script>
  tinymce.init({
    selector:'textarea'
  });          
  tinymce.activeEditor.setContent('custom');
</script>    

Does anyone knows about this problem? Thanks! any effort would be appreciated

I got Cannot read property 'setContent' of null, when i want to using setContent function. Intended for set value in text editor generated by Tinymce library. Is am wrong to implemented it?Below is my snippet code:

<textarea name="content"></textarea>      
<script src="assets/tinymce/tinymce.min.js"></script>
<script>
  tinymce.init({
    selector:'textarea'
  });          
  tinymce.activeEditor.setContent('custom');
</script>    

Does anyone knows about this problem? Thanks! any effort would be appreciated

Share Improve this question edited Mar 7, 2019 at 5:10 Darth Veyda 8882 gold badges12 silver badges23 bronze badges asked Jun 27, 2015 at 4:46 crayoncrayon 811 gold badge1 silver badge11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

You have to wait until the editor is initialized:

tinymce.init({
  selector:'textarea',
  init_instance_callback : function(editor) {
    editor.setContent('custom');
  }
});

Real late to the party, but tinyMCE.init also has setup configuration option which allows to add any event handlers to the specific editor being initialised:

tinymce.init({
        setup: editor => {
            editor.on('init', () => {
                editor.setContent('custom');
            });
        }
    });

There is other turn around for your solution:

Call setContent within $( window ).load(function(){}) but you have initialize jquery first.

Modified code will look like below:

<textarea name="content"></textarea>      
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="assets/tinymce/tinymce.min.js"></script>
<script>
  tinymce.init({
    selector:'textarea'
  }); 
$( window ).load(function(){         
  tinymce.activeEditor.setContent('custom');
});
</script>  
发布评论

评论列表(0)

  1. 暂无评论