为CKEditor添加自定义样式

问题描述:

我最近将CKEditor添加到了我的应用程序中,并且希望在编辑器中包含我自己的CSS样式表,以便我可以在编辑器中选择它们。为CKEditor添加自定义样式

我该如何做到这一点?到目前为止我的代码看起来是这样的:

<script type="text/javascript"> 

    CKEDITOR.replace('editor1',{ 
     uiColor : '#9AB8F3', 
    }); 

</script> 
+4

您可以很容易地将自定义样式添加到编辑器。 [本页](http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Styles)显示了如何。 – 2010-01-20 16:15:22

请看@metavida答案一个更好的答案比这个

<script type="text/javascript"> 

    CKEDITOR.replace('editor1',{ 

      uiColor : '#9AB8F3', 
      stylesSet.add('default', [ 
       { name: 'My Custom Block', element: 'h3', styles: { color: 'Blue'} }, 
       { name: 'My Custom inline style', element: 'q'} 
      ]);  
    }); 

</script> 

但如果你在多个地方使用这一点,” d最好把它放到stylescombo \ styles \ default.js文件中,并根据api更新你的config.js文件。

+8

但这甚至不是有效的JS! – 2011-06-09 12:16:33

+0

@Matti什么不是特别的? – dove 2011-06-16 12:40:00

+3

坐在对象文字中间的方法调用无效。 – 2011-06-16 13:37:40

这里晚会不多,但默认款式在_source/plugins/styles/styles/default.js。你可以使用它作为你的风格配置,并添加样式,它会有效地追加。

<script type="text/javascript"> 
    // This code could (may be should) go in your config.js file 
    CKEDITOR.stylesSet.add('my_custom_style', [ 
    { name: 'My Custom Block', element: 'h3', styles: { color: 'blue'} }, 
    { name: 'My Custom Inline', element: 'span', attributes: {'class': 'mine'} } 
    ]); 
    // This code is for when you start up a CKEditor instance 
    CKEDITOR.replace('editor1',{ 
    uiColor: '#9AB8F3', 
    stylesSet: 'my_custom_style' 
    }); 
</script> 

您也可以阅读stylesSet.add语法的完整文档:

+0

hey op - 选择此答案。 – Bosworth99 2013-05-16 21:57:52

+0

最好的方法是使用这个:http://ckeditor.com/addon/stylesheetparser在下面看到我的答案。 – 2014-03-05 04:08:43

+0

我不同意,这样@joshua。你需要通过css文件中的每个选择器,这意味着为此创建一个专用文件。 – 2014-10-13 21:11:53

正如已经提到的,解释如何建立一套custom styles的页面。什么是小宝石隐藏的页面上(而不是真正清楚)是而不是创建一组命名的样式,可以内嵌在组态中定义的样式,像这样:

stylesSet : [ 
    { 
     name : 'Green Title', 
     element : 'h3', 
     styles : 
     { 
      'color' : 'Green' 
     } 
    } ], 

这对我的作品

CKEDITOR.addCss('body{background:blue;}'); 
+0

正是我在寻找的感谢。 – 2015-03-04 11:16:37

+0

在创建编辑器实例(来自[documentation](http://docs.ckeditor.com/#!/api/CKEDITOR-method-addCss))之前,应该调用该函数**。 – Webars 2016-01-16 11:31:14

最好的办法就是使用这个插件:http://ckeditor.com/addon/stylesheetparser

了 '的CKEditor /插件' 目录中贴吧,然后在您的 '的CKEditor/config.js' 是这样的:

config.extraPlugins = 'stylesheetparser'; 
config.contentsCss = '/css/inline-text-styles.css'; 
config.stylesSet = []; 

其中'/css/inline-text-styles.css'是ckeditor之外的webroot中自己的css文件夹的路径。这可以节省你不得不混合与JavaScript的CSS。

+0

这似乎可能会有所帮助,但你说“你自己的css文件夹”,然后显示一个文件的路径。路径取决于ckeditor本身的位置,还是始终来自root?我无法得到它的工作,我认为这是道路。 – bcsteeve 2015-11-06 19:40:53