如何在CKEditor 5中用正则验证链接?绑定事件遇报错
CKEditor 5链接验证及事件绑定问题
背景
我正在开发一款基于CKEditor的CMS,需要通过正则表达式验证链接有效性。找到一个7年前的同类问题解决方案,但尝试后出现报错。不确定是CKEditor版本更新导致的兼容问题,还是我的配置有误——目前我采用源码构建CKEditor 5的方式,目的是深入理解编辑器的工作机制。
当前编辑器配置代码
ClassicEditor .create( document.querySelector( '#editor' ), { plugins: [ Link, Essentials, Paragraph, Bold, Italic, Alignment ], toolbar: [ 'bold', 'italic', 'link', 'alignment' ], link: { decorators: { openInNewTab: { mode: 'manual', label: 'Open in a new tab', defaultValue: true, // 该选项默认选中 attributes: { target: '_blank', rel: 'noopener noreferrer' } } } } } ) .then( editor => { console.log( 'Editor was initialized', editor ); } ) .catch( error => { console.error( error.stack ); } );
事件绑定尝试及报错
为实现链接验证,我尝试绑定dialogDefinition事件,但遇到以下问题:
- 使用代码:
ClassicEditor.on('dialogDefinition', function (ev) { console.log("Test") });
报错信息:"ClassicEditor.on is not a function"
- 改用代码:
CKEDITOR.on('dialogDefinition', function (ev) { console.log("Test") });
报错信息:CKEDITOR is not defined
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

