twitter-bootstrap - 如何将响应类添加到 ckeditor 图像对话框?

标签 twitter-bootstrap ckeditor colorbox

我有一个带有 CK 编辑器的网站面包师。 如果没有插入任何类,我想让图像自动具有“img-responsive”类。 如何?我可以这样做吗

我找不到这个的字典http://docs.cksource.com/CKEditor_3.x/Howto/Default_Field_Values 每个输入的名称是什么等等?而 html 源代码不会显示这一点。

最佳答案

很久以前我也遇到过同样的问题。我可能复制粘贴了大部分代码,并自己做了一些小修改。它的代码有点长,它将 img-responsive 类添加到所有添加的图像。

 CKEDITOR.replace('editor1');
    CKEDITOR.on('instanceReady', function(ev) {

        //resp. images for bootstrap 3
        ev.editor.dataProcessor.htmlFilter.addRules(
                {
                    elements:
                            {
                                $: function(element) {
                                    // Output dimensions of images as width and height
                                    if (element.name == 'img') {
                                        var style = element.attributes.style;
                                        //responzive images

                                        //declare vars
                                        var tclass = "";
                                        var add_class = false;

                                        tclass = element.attributes.class;

                                        //console.log(tclass);
                                        //console.log(typeof (tclass));

                                        if (tclass === "undefined" || typeof (tclass) === "undefined") {
                                            add_class = true;
                                        } else {
                                            //console.log("I am not undefined");
                                            if (tclass.indexOf("img-responsive") == -1) {
                                                add_class = true;
                                            }
                                        }

                                        if (add_class) {
                                            var rclass = (tclass === undefined || typeof (tclass) === "undefined" ? "img-responsive" : tclass + " " + "img-responsive");
                                            element.attributes.class = rclass;
                                        }

                                        if (style) {
                                            // Get the width from the style.
                                            var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec(style),
                                                    width = match && match[1];

                                            // Get the height from the style.
                                            match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec(style);
                                            var height = match && match[1];

                                            if (width) {
                                                element.attributes.style = element.attributes.style.replace(/(?:^|\s)width\s*:\s*(\d+)px;?/i, '');
                                                element.attributes.width = width;
                                            }

                                            if (height) {
                                                element.attributes.style = element.attributes.style.replace(/(?:^|\s)height\s*:\s*(\d+)px;?/i, '');
                                                element.attributes.height = height;
                                            }
                                        }
                                    }



                                    if (!element.attributes.style)
                                        delete element.attributes.style;

                                    return element;
                                }
                            }
                });
    });

关于twitter-bootstrap - 如何将响应类添加到 ckeditor 图像对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334816/

相关文章:

html - 创建 Bootstrap 面板和样式

ruby-on-rails - Rails] 富文本编辑器上传到 Amazon S3

javascript - 使用 btn-group 切换图像集

css - 在 CKEditor 中设置默认图像样式

javascript - 从 ckeditor 5 获取 xml

javascript - 无法修复弹出元素的宽度(colorbox jquery 覆盖)

jquery - 在页面加载时打开 Colorbox

javascript - 缺少部分 jQuery 代码?

jquery - Bootstrap CSS 动态表格 Accordion

css - Bootstrap 3 和 IE8 的问题(表现得像移动设备)——包括响应