CKEditor自动从div中剥离类 [英] CKEditor automatically strips classes from div

查看:167
本文介绍了CKEditor自动从div中剥离类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上使用 CKEditor 作为后端编辑器。它驱动我的弯曲虽然,因为它似乎想改变代码,它认为适合每当我按下源按钮。例如,如果我命中源并创建< div> ...

I am using CKEditor as a back end editor on my website. It is driving me round the bend though as it seems to want to change the code to how it sees fit whenever I press the source button. For example if I hit source and create a <div>...

<div class="myclass">some content</div>

然后没有明显的理由从< div> ,因此当我再次点击源时,它已改为...

It then for no apparent reason strips the class from the <div>, so when I hit source again it has been changed to...

<div>some content</div>

我假设这个刺激行为可以在 config.js ,但我一直在挖掘,没有找到任何文档,以关闭它。

I presume this irritating behaviour can be turned off in the config.js, but I have been digging and cant find anything in documentation to turn it off.

推荐答案

过滤



最简单的解决方案是转到 config.js 和设置:

config.allowedContent = true;

请记住清除浏览器的缓存)。然后CKEditor停止对输入的内容进行过滤。但是,这将完全禁用内容过滤,这是最重要的CKEditor功能之一。

(Remember to clear browser's cache). Then CKEditor stops filtering the inputted content at all. However, this will totally disable content filtering which is one of the most important CKEditor features.

您还可以配置 CKEditor的内容过滤器更精确地只允许您需要的这些元素,类,样式和属性。这个解决方案是更好的,因为CKEditor仍然会删除很多笨重的HTML,浏览器在复制和粘贴内容时产生,但它不会剥离你想要的内容。

You can also configure CKEditor's content filter more precisely to allow only these element, classes, styles and attributes which you need. This solution is much better, because CKEditor will still remove a lot of crappy HTML which browsers produce when copying and pasting content, but it will not strip the content you want.

例如,您可以扩展默认CKEditor的配置以接受所有div类:

For example, you can extend the default CKEditor's configuration to accept all div classes:

config.extraAllowedContent = 'div(*)';

或一些Bootstrap材料:

Or some Bootstrap stuff:

config.extraAllowedContent = 'div(col-md-*,container-fluid,row)';

或者您可以允许使用可选的 dir 属性 dt dd 元素:

Or you can allow description lists with optional dir attributes for dt and dd elements:

config.extraAllowedContent = 'dl; dt dd[dir]';

这些只是非常基本的例子。您可以编写所有类型的规则 - 需要属性,类或样式,只匹配特殊元素,匹配所有元素。你也可以禁止东西,完全重新定义CKEditor的规则。
详细了解:

These were just very basic examples. You can write all kind of rules - requiring attributes, classes or styles, matching only special elements, matching all elements. You can also disallow stuff and totally redefine CKEditor's rules. Read more about:

  • Content filtering in CKEditor – why do you need content filter.
  • Advanced Content Filter – in deep description of the filtering mechanism.
  • Allowed content rules – how to write allowed content rules.

这篇关于CKEditor自动从div中剥离类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆