如何配置 ckeditor 不将内容包装在
中堵塞?
I am using ckeditor on my website to make it easier for users to input HTML.
However, the data I get back from ckeditor is wrapped in <p></p>
blocks. (Which I don't want.)
Is there some configuration setting that forces the editor to not wrap the text in anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将以下内容添加到 CKEditor 的 config.js 文件中:
示例:
详细信息
控制此行为的配置设置基于您希望在用户按 Enter 时发生的情况。
为了防止刚接触 HTML 的人阅读本文,我对所涉及的概念以及为什么在按下 Enter 键时需要插入标签进行了一些基本解释。
我们知道,如果我们在 HTML 文档中输入一些文本,然后在新行中添加其他文本,浏览器不会将文本显示为两行,它会忽略任何回车符,并将字符之间的多个空格压缩为单个空格空间。
以下 HTML:
将呈现为:
因此编辑器需要插入一个 HTML 标签来告诉浏览器应该在新行上显示第二组文本。
控制此操作的配置设置是 config.enterMode,它提供三个选项:
1 - 插入段落
默认设置每次按 Enter 时都会创建一个段落元素:
2 - 插入'div'
您可以选择创建
div
元素而不是段落:3 - 插入中断(您正在寻找的设置)
如果您不想将文本换行,您可以选择插入换行标记:
CKEditor 文档表明使用
ENTER_BR
不建议设置:另一个相关设置“autoParagraph”
还有第二个设置可以控制类似的情况 –
config.autoParagraph
。它的功能取决于上面讨论的 config.enterMode 设置。autoParagraph
确定内联元素(例如span
)是否包装在由 指定的块元素(p
或div
)中enterMode
设置。默认情况下是包裹内联元素,因此如果您输入这样的跨度(作为 HTML):它将被包裹在 ap 或 div 元素中,如下所示:
或这样:
如果将其设置为 <,则内联元素将不会被包裹code>false 或者将
enterMode
设置为CKEDITOR.ENTER_BR
。CKEditor 文档包含有关 config.autoParagraph 的注释:
更多设置 还有
另外三个设置与此主题有些相关:
config.fillEmptyBlocks
config.forceEnterMode
config.ignoreEmptyParagraph
参考
完整列表可用的配置选项可以在这里找到:
Add the following to your config.js file for CKEditor:
Example:
Details
The configuration setting that controls this behavior is based on what you want to happen when the user presses Enter.
Just in case someone who's new to working with HTML reads this, I'm including some basic explanation of the concepts involved and why a tag will need to be inserted when the Enter key is pressed.
We know that if we enter some text into an HTML document and then put additional text on a new line, the browser won't display the text as two lines, it will ignore any carriage returns and will condense multiple spaces between characters to a single space.
The following HTML:
Will be rendered as:
So the editor needs to insert an HTML tag to tell the browser that it should display the second group of text on a new line.
The configuration setting that controls this is
config.enterMode
and it offers three options:1 - Insert paragraph
The default setting creates a paragraph element each time Enter is pressed:
2 - Insert 'div'
You can choose to create a
div
element instead of a paragraph:3 - Insert break (the setting you're looking for)
If you prefer to not wrap the text in anything, you can choose to insert a line break tag:
The CKEditor documentation indicates that using the
ENTER_BR
setting is not recommended:Another related setting 'autoParagraph'
There is a second setting that controls a similar situation –
config.autoParagraph
. How it functions depends on theconfig.enterMode
setting discussed above.autoParagraph
determines whether inline elements such asspan
are wrapped in the block element (p
ordiv
) specified by theenterMode
setting. The default is to wrap inline elements, so if you enter a span like this (as HTML):It will be wrapped in a p or div element like this:
or this:
The inline element won't be wrapped if you set this to
false
or if you setenterMode
toCKEDITOR.ENTER_BR
.The CKEditor documentation includes this note about
config.autoParagraph
:Even more settings
There are three more settings that are somewhat related to this subject:
config.fillEmptyBlocks
config.forceEnterMode
config.ignoreEmptyParagraph
Reference
A complete list of the available configuration options can be found here:
我知道我有点晚了,但我认为OP正在寻找的选项是:
I know I'm a little late to the game, but I think the option the OP is looking for is:
上面已经很好地回答了这个问题,但是正如前面提到的,您不应该在主配置中真正更改它。
正确的方法是按 .replace 执行此操作。
IE
This is answered perfectly well above, however as mentioned you should not really be changing this in the main config.
The correct way to do this is per .replace really.
i.e.
一个非常简单的解决方案,无需更改任何配置,就是使用
shift
+enter
进行换行
,然后enter
会产生一个新段落。优点是您不必进行任何配置更改。另外,你两者都有。
A very simple solution without any configuration change is to use
shift
+enter
for a line break<br>
, andenter
would cause a new paragraph.Advantage is that you don't have to do any configuration changes. Plus, you have both.
如果您想排除
标签,并且只需要 Ckeditor 中的基本编辑工具(如粗体斜体、上标、下标等),请按照以下步骤操作:
我对此 100% 确定我连续研究了 36 小时:)
第 1 步:在 PHP 网页中添加此脚本
第 2 步:添加
id="editor2"
和onfocus="this.value='';"< /code> 在您的文本区域中,如下所示
步骤 3:确保从文本区域中删除
Class="ckeditor"
。步骤 4:如果没有发生,请重新加载网页,删除缓存/历史记录并重新启动 PC/笔记本电脑。
第 5 步:完成:)
If you want to exclude
<p>
tag and want only basic editing tool like Bold Italic superscript Subscript etc in Ckeditor then follow these steps:I am 100% sure about this as I researched 36 Hours continuously :)
Step 1: Add this script in your PHP webpage
Step 2: add
id="editor2"
andonfocus="this.value='';"
in your textarea like thisStep 3: Be sure that remove
Class="ckeditor"
from Textarea.Step 4: Reload your webpage if not happened Delete Cache/History and Restart PC/laptop.
Step 5: Its Done :)
对于 Django-ckeditor,请在您的settings.py 文件中添加此配置:
For
Django-ckeditor
add this config in yoursettings.py
file:如果有人使用 ckeditor 5,请不要寻找此选项。他们已经删除了它,我花了好几天的时间来解决这个问题。
相关 GitHub 问题
If anyone comes here with ckeditor 5, don't look for this option. They have removed it, I've spent days tyring to figure this out.
Related GitHub issue