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

javascript - How to get the contents of ckeditor on change? - Stack Overflow

programmeradmin2浏览0评论

I have the following code for detecting changes on the ckeditor surface.

CKEDITOR.instances['editor1'].on("instanceReady", function(){                    
this.document.on("keyup", function(){
console.log("sth changed");
});
});

I would like to get the contents of editor on change for counting the words in it. How can I reach it using CKEDITOR.instances?

I have the following code for detecting changes on the ckeditor surface.

CKEDITOR.instances['editor1'].on("instanceReady", function(){                    
this.document.on("keyup", function(){
console.log("sth changed");
});
});

I would like to get the contents of editor on change for counting the words in it. How can I reach it using CKEDITOR.instances?

Share Improve this question edited May 16, 2013 at 10:46 codewaggle 4,9432 gold badges33 silver badges48 bronze badges asked May 15, 2013 at 8:24 Rápli AndrásRápli András 3,9231 gold badge37 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

The only correct way to retrieve contents of the editor is:

CKEDITOR.instances.yourInstanceName.getData();

You can also try this.on( 'key', function() { ... } ); because editor fires key events.

Additionally I remend you the ticket regarding onchange event and monitoring changes in the editor.

And the last but not least: The wordcount plugin which has already been implemented and does your job ;)

For version 5 you use editor.model.document.on('change:data', ...).

So much like:

let someContent = '';

ClassicEditor.create(document.querySelector('#editor')).then(editor => {
  editor.model.document.on('change:data', () => {
    someContent = editor.getData()
  })
}).catch(err => {
  console.log('error loading ckeditor', err);
})

Read the docs: https://ckeditor./docs/ckeditor5/latest/builds/guides/integration/basic-api.html#listening-to-changes

There is an "onChange" plugin from @AlfonsoML in the plugins list:
onChange

It covers a lot more than just "keyUp". You can read more on his blog, where he describes different events that are captured:
onChange event for CKEditor

Try this:

    CKEDITOR.instances["CKEditor"].on('change', function() { 
        console.log(this.getData());
    });

You can use this:

<script>   
    CKEDITOR.replace( 'editor1' );
    CKEDITOR.instances.editor1.on("change", function() {
        $("#content-val").html(this.getData())//show content inside #content-val div
    });
</script>
发布评论

评论列表(0)

  1. 暂无评论