javascript - CKeditor 将类添加到 img 标签

标签 javascript jquery html css ckeditor

我正在尝试向 CKeditor 中任何插入的 img 标签添加一个类。我尝试了各种方法,但似乎无法弄清楚这个插件的设置是如何工作的。文档一大堆,只说要加代码,没说加在哪里,文件一大堆。

我尝试将它添加到 config.js 的底部

/**
 * @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

CKEDITOR.editorConfig = function( config ) {
    // Define changes to default configuration here.
    // For complete reference see:
    // http://docs.ckeditor.com/#!/api/CKEDITOR.config

    // The toolbar groups arrangement, optimized for two toolbar rows.
    config.toolbarGroups = [
        { name: 'clipboard',   groups: [ 'clipboard', 'undo' ] },
        { name: 'editing',     groups: [ 'find', 'selection', 'spellchecker' ] },
        { name: 'links' },
        { name: 'insert' },
        { name: 'forms' },
        { name: 'tools' },
        { name: 'document',    groups: [ 'mode', 'document', 'doctools' ] },
        { name: 'others' },
        '/',
        { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
        { name: 'paragraph',   groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ] },
        { name: 'styles' },
        { name: 'colors' },
        { name: 'about' }
    ];

    // Remove some buttons provided by the standard plugins, which are
    // not needed in the Standard(s) toolbar.
    config.removeButtons = 'Underline,Subscript,Superscript';

    // Set the most common block elements.
    config.format_tags = 'p;h1;h2;h3;pre';

    // Simplify the dialog windows.
    config.removeDialogTabs = 'image:advanced;link:advanced';
    config.extraPlugins = 'confighelper';   

    config.stylesSet = 'my_styles';

};

CKEDITOR.stylesSet.add( 'my_styles', [

    { name: 'Custom Image', element: 'img', attributes: { 'class': 'myClass' }}
]);

那没用

所以我尝试将它添加到实际的 html 页面中

<script>
CKEDITOR.stylesSet.add( 'my_styles', [

    { name: 'Custom Image', element: 'img', attributes: { 'class': 'myClass' }}
]);
</script>

那也没用

阅读他们的文档我无法理解它 http://docs.ckeditor.com/#!/guide/dev_howtos_styles

如何向通过编辑器添加的任何 img 标签添加类?

最佳答案

我没有使用 CKEDITOR,但问题可能是 stylesSet 未在 CKEDITOR 调用中声明,因为它稍后定义。 尝试将 CKEDITOR.stylesSet.add 移动到 editorConfig 之前。

或者将您的样式放入第一个代码块:

CKEDITOR.editorConfig = function( config ) {

   ...
   ...

   config.stylesSet = [
      { name: 'Custom Image', element: 'img', attributes: { 'class': 'myClass' }}
   ];

};
</script>

还有一些关于用法的文档 http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-stylesSet

关于javascript - CKeditor 将类添加到 img 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26181100/

相关文章:

Jquery图像一张一张淡入淡出?

javascript - NxN 棋盘的 TicTacToe 获胜逻辑

javascript - 在 meteor 中制作计算器

javascript - 掌握预加载图像

javascript - 如何用视频标签替换标签

jquery - 使用 jQuery 悬停的不透明图像

html - 大于和小于的 CSS nth-child

javascript - onclick 功能不起作用(更改样式)

javascript - 按下侧箭头时将焦点设置到下一个输入类型

javascript - JS curry 递归处理空参数