CKEditor允许您通过编辑文件styles.js向样式组合框添加自定义样式(有关详细信息,请参阅What is a good javascript HTML editor for adding custom HTML elements?)
我想在工具栏中添加唯一按钮来应用我的自定义样式,而不是用户必须从样式组合中选择它们。
如何向CKEditor工具栏添加自定义按钮?
答案 0 :(得分:3)
继续使用以下代码:
// Your custom style.
var myStyle = new CKEDITOR.style( {
element: 'span',
attributes: {
'data-foo': 'bar',
'class': 'myClass'
},
styles: {
color: 'red'
}
} );
CKEDITOR.replace( 'editor1', {
on: {
// Register command and button along with other plugins.
pluginsLoaded: function() {
var editor = this;
// Registers a command that applies the style.
// Note: it automatically adds Advanced Content Filter rules.
this.addCommand( 'myStyle', new CKEDITOR.styleCommand( myStyle ) );
// Add toolbar button for this command.
this.ui.addButton && this.ui.addButton( 'myStyleButton', {
label: 'My style',
command: 'myStyle',
toolbar: 'insert,10'
// You may want to set some icon here.
// icon: 'someIcon'
} );
}
}
} );