强制CKEditor将类添加到p-tags

dwa*_*orf 9 css ckeditor

我必须配置CKEditor以将class-attribute添加到内容中的每个p-tag.您可以使用config.format_p执行类似的操作,但它只会将class-attribute应用于标记为"normal"的文本,而不是默认值.

任何人?

编辑:我正在使用当前版本3.6.2.以下是我的配置的相关部分:

CKEDITOR.editorConfig = function( config )
{   
    config.removeFormatTags = 'b,div,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var,form,input,textarea';

    config.format_p =
    {
        element: 'p',
        attributes:
        {
            'class': 'tiny_p'
        }
    };
    config.skin = "office2003";
    config.entities_processNumerical = true;
}
Run Code Online (Sandbox Code Playgroud)

config.format_p选项仅在用户从格式菜单中选择"正常"时生效,并且config.removeFormatTags仅在用户手动单击清洁按钮时才有效.两者都应该像在TinyMCE中那样自动工作.

Mil*_*ric 10

您可以添加html处理器过滤器

editor.dataProcessor.htmlFilter.addRules({
    elements :{
        p : function( element ){
            if ( element.className.indexOf("thiny_p") < 0){
                element.className += 'thiny_p';
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

此外,如果在将内容发送到服务器之前不需要将其创建为ckedito插件,则可以使用jQuery更改内容

$("iframe", "#cke_editor1").contents().find("p").addClass("tiny_p");
Run Code Online (Sandbox Code Playgroud)

或者,如果textarea(来源)有效

var editor= $("textarea", "#cke_editor1"); 
editor.val(editor.val().replace(/<p>/gi, "<p class='thiny_p'>"))
Run Code Online (Sandbox Code Playgroud)

你应该调整一点.replace(/<p>/gi, "<p class='thiny_p'>")正则表达式来支持其他情况.

编辑

终于有时间在我的盒子上下载和设置编辑器,这里是工作插件

CKEDITOR.plugins.add( 'customparagraph',
{
    init: function( editor )
    {
        editor.addCommand('addParagraphClassName',{
            exec : function( editor ){    
                var ps = editor.document.$.getElementsByTagName("p");
                for (var i=0; i < ps.length; i++){

                    if(ps[i].className.indexOf("thiny_p") < 0){
                        ps[i].className += "thiny_p";
                    }

                }

            }
        });

        editor.ui.addButton( 'ThinyP',{
            label: 'Appends thiny_p class',
            command: 'addParagraphClassName',
            icon: this.path + 'images/icon.gif'
        });
    }
} );
Run Code Online (Sandbox Code Playgroud)

把它放在plugins/customparagraph/plugin.js 也创建图标图像plugins/customparagraph/images/icon.gif

在配置中,您必须添加以下配置选项config.js您的CKEdito安装

CKEDITOR.editorConfig = function( config )
{
    config.extraPlugins = "customparagraph";
    config.toolbar = [ [ 'ThinyP' ] ]; // add other toolbars and keep in mid this can be overwritten in page which loads CKEditor
};
Run Code Online (Sandbox Code Playgroud)

要么

在加载CKEditor的页面中

<script type="text/javascript">
//<![CDATA[
    // Replace the <textarea id="editor1"> with a CKEditor
    // instance, using default configuration.
    CKEDITOR.replace( 'editor1',
        {
            extraPlugins : 'customparagraph',
            toolbar :
            [
                [ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],
                [ 'ThinyP' ]
            ]
        });
//]]>
</script>
Run Code Online (Sandbox Code Playgroud)

用户必须在保存前单击工具栏按钮