CKEditor5中使用CKEDITOR类报错:Uncaught ReferenceError: CKEDITOR is not defined
问题原因与解决方法
你遇到的问题核心是版本混淆:
- 你引入的是CKEditor 5(从CDN地址
ckeditor5/38.1.1可判断),但CKEDITOR.replace()是CKEditor 4的专属API。CKEditor 5完全重构了代码结构,不存在CKEDITOR这个全局对象,所以会抛出CKEDITOR is not defined的错误。 - 你的代码里已经正确使用了CKEditor 5的初始化方式
ClassicEditor.create(),但后面叠加了CKEditor 4的代码,导致冲突。
解决步骤
- 彻底删除错误的
CKEDITOR.replace()代码块,它和CKEditor 5完全不兼容。 - 如果要配置
extraAllowedContent(对应CKEditor 5的内容过滤规则),直接把配置项放到ClassicEditor.create()的第二个参数里。
修改后的完整代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CKEditor</title> <script src="https://cdn.ckeditor.com/ckeditor5/38.1.1/classic/ckeditor.js"></script> </head> <body> <div id="editor">This is some sample content.</div> <script> ClassicEditor .create( document.querySelector( '#editor' ), { // CKEditor 5中对应extraAllowedContent的配置 allowedContent: 'style;*[id,rel](*){*}' }) .then( editor => { console.log( editor ); }) .catch( error => { console.error( error ); }); </script> </body> </html>
注意:CKEditor 5和CKEditor 4的API、配置体系完全不同,查找资料时要确认内容对应的CKEditor版本,避免混用。
内容的提问来源于stack exchange,提问作者KANAYO AUGUSTIN UG
相关产品推荐
相关产品推荐

