javascript - 向 CKEditor 添加自定义样式的按钮

标签 javascript ckeditor

CKEditor 允许您通过编辑 styles.js 文件将自定义样式添加到样式组合框(有关详细信息,请参阅 What is a good javascript HTML editor for adding custom HTML elements?)

我想向工具栏添加独特的按钮来应用我的自定义样式,而不是用户必须从样式组合中选择它们。

如何向 CKEditor 工具栏添加自定义按钮?

最佳答案

继续执行以下代码:

// 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'
            } );
        }
    }
} );

关于javascript - 向 CKEditor 添加自定义样式的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18054351/

相关文章:

Javascript - pdf 页面上的标记位置不正确

php - 如何停止更改代码?

php - CKFinder 3 无法上传文件

javascript - 如果使用目录 :'rtl',Dojo Text 会将所有特殊字符右对齐

javascript - 阻止对父级的相同域 iframe 访问

javascript - 使用 CKEditor 在 setData 和模式更改时重置监听器事件

javascript - 生成代码上带有工具栏的内联 CKEditor

javascript - 从 ckeditor 5 获取 xml

javascript - 从 HTTP.get 访问变量

javascript - 将 React 组件字符串转换为 JSX