javascript - CKEditor 焦点 CSS 更改

标签 javascript symfony ckeditor

我在 symfony 项目中使用 CKEditor 和 FOS\CKEditor-bundle 1.2。我希望包含 CKEditor 的整个元素具有焦点边框,类似于您在 Stackoverflow 上编写或回答问题的方式。 使用较旧的 question和一个 JSFiddle,我已经做到了这一点:

JS

// Set focus and blur listeners for all editors to be created.
CKEDITOR.on( 'instanceReady', function( evt ) {
    var editor = evt.editor,
        body = CKEDITOR.document.getBody();

    editor.on( 'focus', function() {
        // Use jQuery if you want.
        body.addClass( 'fix' );
    } );

    editor.on( 'blur', function() {
        // Use jQuery if you want.
        body.removeClass( 'fix' );
    } );    
} );

CKEDITOR.replace( 'editor', {
    plugins: 'wysiwygarea,sourcearea,basicstyles,toolbar',
    on: {
        // Focus and blur listeners can be set per-instance,
        // if needed.
        // focus: function() {},
        // blur: function() {}
    }
} );

或者作为 demo of JSFiddle 。在 fiddle 上一切看起来都很好,但是在我的 CKEditor 版本中,边框实际上设置在主页的 body 上,并且没有 CKEditor。

我不明白为什么它不起作用或者需要改变什么。有想法吗?

最佳答案

CKEDITOR.document 等同于 window.document(不一样,因为)。如果您想将类添加到特定编辑器实例的 body 元素,请使用 editor.document.getBody();

另一个问题是编辑器实例的 body 保存在内部文档中,因此如果您希望 fix 类发挥作用,您需要将其添加到例如ckeditor/contents.css 或分配给 https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html#cfg-contentsCss 的文件.

我还建议将 body 规则中默认的 margin:20px; 替换为 padding: 20px;边距:0;

编辑:

根据您的评论,我认为更好的方法是:

        <style>
            .fix {
                border: 5px solid black !important;
            }
        </style>
        ...
        var editor = CKEDITOR.replace( 'editor1', {/*instance configuration goes here*/});
        CKEDITOR.on( 'instanceReady', function( evt ) {

            var main_container = document.getElementById( 'cke_' + evt.editor.name ),
                content_container = main_container.getElementsByClassName( 'cke_contents' )[0];

                editor.on( 'focus', function() {
                    content_container.classList.add( "fix" );
                } );

                editor.on( 'blur', function() {
                    content_container.classList.remove( "fix" );
                } ); 
        } );

注释:

  • 样式规则可以放入CSS文件中,但在这种情况下,这需要是主页CSS文件,而不是contents.css
  • 有必要将 !important 添加到修复类中的边框,因为编辑器的 cke_reset 将其设置为 0。您也可以将其添加为内联样式。<

这是一个工作示例:https://codepen.io/j_swiderski/pen/XyMyme

关于javascript - CKEditor 焦点 CSS 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53382451/

相关文章:

javascript - 使用 javascript 将多行、缩进的 json 转换为单行

php - Doctrine 映射到未知实体

angular - 如何在Angular项目中集成CKEditor的上传图片插件

javascript - chrome 中的 CKEDITOR 在光标位置添加 "?"标记,同时应用任何样式(粗体/斜体)而不选择

javascript - CSS 宽度转换不够快,与 translateX 结合时会导致 UI 抖动

javascript - Angular 嵌入和范围

javascript - 按键合并带下划线的数组

html - 撇号转换为 & # 039 ;在 Twig 上

php - docker不能通过symfony和monolog捕获php-fpm输出

angular - 我们如何在 ANGULAR 6 中将 ckfinder 集成到 ckeditor 中?