CKEditor继承链接标记中的字体样式

时间:2014-04-17 10:59:01

标签: html hyperlink ckeditor fckeditor

我需要强制CKEditor将字体样式应用于链接标记而不是内部span标记。例如,当您编写一些文本时,添加指向此文本的链接,并更改链接的颜色,源代码如下所示:

<a href="http://www.google.com" target="_blank"><span style="color: rgb(221, 160, 221);">Click here...</span></a>

我需要像这样生成它:

<a style="color: rgb(221, 160, 221);" href="http://www.google.com" target="_blank"><span>Click here...</span></a>

或者像这样:

<a style="color: rgb(221, 160, 221);" href="http://www.google.com" target="_blank"><span style="color: rgb(221, 160, 221);">Click here...</span>

我可以为此创建内容过滤器,但我不想放弃最初在链接标记中的内容

1 个答案:

答案 0 :(得分:0)

看起来我解决了......

对我来说,看起来没有别的,但是要添加htmlFilter规则。

是这样的:

CKEDITOR.on('instanceReady', function (ev) {   
    ev.editor.dataProcessor.htmlFilter.addRules(
                {
                    elements:
                    {
                        $: function (element) {

                            if (element.name == 'a') {
                                if (element.children.length == 1) {
                                    if (element.children[0].name == 'span') {
                                        var innerSpan = element.children[0];
                                        if (innerSpan.attributes.style) {
                                            element.attributes.style = innerSpan.attributes.style;                                            
                                        }
                                    }
                                }
                            }

它检查链接是否只包含一个span子项,以及何时只复制样式属性中的所有内容。