无法将事件处理程序附加到 ClassicEditor
Not able to attach event handler to ClassicEditor
我正在使用 CKEditor 来允许用户设置 post 消息的样式,并且我正在使用以下 javascript 代码来配置我的 ClassicEditor:
ClassicEditor
.create($("#post-message")[0], {
language: $("html").attr("lang"),
alignment: {
options: ['left', 'right']
},
toolbar: ["undo", "redo", "bold", "italic", "blockQuote", "link", "numberedList", "bulletedList"],
autoParagraph : false
})
.then(editor => {
myEditor = editor;
myEditor.model.document.on('change', (event) => {
$("#post-message").val(myEditor.getData());
if (validatePostMessage() == false) {
$(".ck-content").css("border-color", "red");
}
else {
$(".ck-content").css("border-color", "rgb(196, 196, 196)")
}
});
//this one not working
myEditor.model.document.on('blur', (event) => {
alert("hi");
});
})
.catch(error => {
console.error(error);
});
以上代码在 change 事件发生时有效,但 blur 事件无效。
<textarea type="text"
class="form-control"
maxlength="2000"
id="post-message"
name="post-message">
</textarea>
我的问题是为什么当我的编辑器失去焦点时模糊事件处理程序不起作用?
PS:
我使用的是以下版本:
https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js
您可以在编辑器中使用 focus tracking 来确定它是聚焦还是模糊。
文档中的示例:
editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
console.log( `View document is focused: ${ isFocused }.` );
});
因此,对于您的用例,它将是:
editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
if ( isFocused == false ) // blur
{
alert('blur');
}
});
我正在使用 CKEditor 来允许用户设置 post 消息的样式,并且我正在使用以下 javascript 代码来配置我的 ClassicEditor:
ClassicEditor
.create($("#post-message")[0], {
language: $("html").attr("lang"),
alignment: {
options: ['left', 'right']
},
toolbar: ["undo", "redo", "bold", "italic", "blockQuote", "link", "numberedList", "bulletedList"],
autoParagraph : false
})
.then(editor => {
myEditor = editor;
myEditor.model.document.on('change', (event) => {
$("#post-message").val(myEditor.getData());
if (validatePostMessage() == false) {
$(".ck-content").css("border-color", "red");
}
else {
$(".ck-content").css("border-color", "rgb(196, 196, 196)")
}
});
//this one not working
myEditor.model.document.on('blur', (event) => {
alert("hi");
});
})
.catch(error => {
console.error(error);
});
以上代码在 change 事件发生时有效,但 blur 事件无效。
<textarea type="text"
class="form-control"
maxlength="2000"
id="post-message"
name="post-message">
</textarea>
我的问题是为什么当我的编辑器失去焦点时模糊事件处理程序不起作用?
PS:
我使用的是以下版本:
https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js
您可以在编辑器中使用 focus tracking 来确定它是聚焦还是模糊。
文档中的示例:
editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
console.log( `View document is focused: ${ isFocused }.` );
});
因此,对于您的用例,它将是:
editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
if ( isFocused == false ) // blur
{
alert('blur');
}
});