在ckeditor 4的时候绑定blur事件代码如下:
window.editor.on('blur',function (){ //这里写你想做的事儿 })
同样的代码搬到ckeditor 5 不仅不能用,还报错。
目前ckeditor5的代码如下:
html部分
<div id='editor'></textarea>
js部分
ClassicEditor
.create( document.querySelector( '#editor' ), {} )
.then( editor => {
editor.on("blur", function(){alert("hello world");});
} )
.catch( err => {
console.error( err.stack );
} );
[user]
解决办法
editor.ui.focusTracker.on( 'change:isFocused', ( evt, name, isFocused ) => {
if ( !isFocused ) {
// 这里写你的业务逻辑,参考下面获取编辑器内容
console.log( editor.getData() );
}
} );
完整代码
ClassicEditor .create( document.querySelector( '#editor' ), {} ) .then( editor => { editor.on("blur", function(){alert("hello world");}); //重点 editor.ui.focusTracker.on( 'change:isFocused', ( evt, name, isFocused ) => { if ( !isFocused ) { // 这里写你的业务逻辑,参考下面获取编辑器内容 console.log( editor.getData() ); } } ); } ) .catch( err => { console.error( err.stack ); } );
[/user]
从上面代码看,ckeditor4 和ckeditor5 版本的配置bulr事件区别还是蛮大的。
http://blog.xqlee.com/article/ckeditor5-blur.html