CKEDITOR添加样式

时间:2013-03-14 09:05:51

标签: css ckeditor

我正试图以编程方式为CKEDITOR 4设置一些css。 这是因为当一个新用户来到编辑器时,我想用不同的颜色来识别他。

在注册的样式数组中,我确实看到了添加的行(CKEDITOR.stylesSet.registered.user0)。但在编辑器中看不到样式? 为什么是这样?我做错了什么?

setStyling: function(editor)
{
    var userIds = this.getUserIds(editor);// returns an array with id's
    var colors = editor.config.Colors;    // Colors are set in the config

    for(var i = 0; userIds.length > i; i++)
    {
        if(CKEDITOR.stylesSet.get('user' + i) === null)
        {
            CKEDITOR.stylesSet.add('user' + i, [
                { name: 'userColor', element: 'span[data-uid="' + userIds[i] +'"]', styles: { color: colors[i] }}
            ]);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

另一种方法完成了这项工作:

    var $head = $("iframe").contents().find("head");
    var $style = $("iframe").contents().find("#uniqueStyle");
    var css = "";

    for(var i = 0; userIds.length > i; i++)
    {
        css += "span[data-uid='" + userIds[i] + "'] { color: " + colors[i] + "; } ";
    }

    if($style)
    {
        $style.remove();
    }

    $('<style id="uniqueStyle" type="text/css"></style>')
        .html(css)
        .appendTo($head);