CKEditor中有用的方法和事件
调用或初始化CKeditor
CKEDITOR.replace('myckeditor');
检查CKEditor是否可用var isCkeditor = typeof CKEDITOR.instances[myckeditor] != 'undefined';
销毁或删除CKeditor实例if(typeof CKEDITOR.instances[myckeditor] != 'undefined') {
setTimeout(function () {
CKEDITOR.instances[myckeditor].updateElement();
CKEDITOR.instances[myckeditor].destroy();
}, );
}
粘贴事件CKEDITOR.on('instanceReady', function(ev) {
ev.editor.on('paste', function(evt) {
if (evt.data.type == 'html') {
// when paste as plain text or direct paste in ckeditor body.
}
if (evt.data.type == 'text') {
// when paste as text or paste from word.
}
});
});
获得焦点或失去焦点事件CKEDITOR.on('instanceReady', function(ev) {
ev.editor.on('focus', function(evt) {
console.log(“Editor focused”)
});
ev.editor.on('blur', function(evt) {
console.log(“Editor blur”)
});
});
最大化或最小化事件CKEDITOR.on('instanceReady', function(ev) {
ev.editor.on('maximize', function(evt) {
console.log('maximized');
});
ev.editor.on('minimize', function(evt) {
console.log('minimized');
});
});
定义对话框事件CKEDITOR.on('dialogDefinition', function (ev) {
});
图片对话框CKEDITOR.on('dialogDefinition', function(ev) {
if (ev.data.name == 'image') {}
});
表格对话框CKEDITOR.on('dialogDefinition', function(ev) {
if (ev.data.name == 'table') {}
});
获取当前对话框,对话框标签页以及标签页组件CKEDITOR.on('dialogDefinition', function(ev) {
if (ev.data.name == 'image') {
var infoTab = ev.data.definition.getContents('info'); //get contents of infotab in image dialog
var browseBtn = infoTab.get('browse'); //get specific element in info tab
//here do some action (onclick, focus) of element
}
});
设置对话框中输入的值var CurrentDialog = CKEDITOR.dialog.getCurrent();
CurrentDialog.selectPage('info');
var urlInput = CurrentDialog.getContentElement('info', 'txtUrl');
urlInput.setValue('pic/flowers.jpg');
获取对话框中输入的值urlInput.getValue();
强制应用js脚本或css样式表文件var head = CKEDITOR.instances.Content.document.getHead();
var myscript = CKEDITOR.document.createElement('link', {
attributes: {
type: 'text/css',
rel: 'stylesheet',
'href': 'mytemp.css'
}
});
head.append(myscript);
工具栏按钮按下事件CKEDITOR.on('instanceReady', function(ev) {
ev.editor.on('afterCommandExec', function(event){
if (event.data.name == 'unlink')
alert("unlink button pressed!");
});
};
双击事件(例子中包含屏蔽双击超链接弹出对话框的代码)
CKEDITOR.on('instanceReady', function(ev) {
ev.editor.on('doubleclick', function(evt){
var element = evt.data.element;
if (element.is('a')){
evt.stop(); // don't do the other listeners
// optionally put your code
}
});
};
[via]http://www.c-sharpcorner.com/blogs/useful-methods-and-events-in-ckeditor[/via]
评论