Html 在 CKEditor 3.0 中关闭封闭的 <p> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1977791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:42:41  来源:igfitidea点击:

Turn off enclosing <p> tags in CKEditor 3.0

htmltagsckeditorparagraph

提问by Kosi2801

Is there a possibility to turn off the automatic enclosing of all written content within <p></p> in CKEditor 3.x?

是否有可能在 CKEditor 3.x 中关闭 <p></p> 内所有书面内容的自动封闭?

I tried

我试过

  CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;

but this just changes the inline linebreaks to <br /> while leaving the enclosing paragraph.

但这只是将内联换行符更改为 <br /> 而离开封闭段落。

Currently writing "Test" produces this output

当前正在编写“测试”会产生此输出

<p>
    Test</p>

but I want it to be simply

但我希望它很简单

Test

Is there a configuration property for this or would another inline editor to be better suited for this?

是否有一个配置属性,或者另一个内联编辑器更适合这个?

回答by Maksymilian Majer

CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;- this works perfectly for me. Have you tried clearing your browser cache - this is an issue sometimes.
You can also check it out with the jQuery adapter:

CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;- 这对我来说非常有效。您是否尝试过清除浏览器缓存 - 这有时是一个问题。
您还可以使用 jQuery 适配器查看它:

<script type="text/javascript" src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/js/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript">
$(function() {
    $('#your_textarea').ckeditor({
        toolbar: 'Full',
        enterMode : CKEDITOR.ENTER_BR,
        shiftEnterMode: CKEDITOR.ENTER_P
    });
});
</script>


UPDATE according to @Tomkay's comment:


根据@Tomkay 的评论更新:

Since version 3.6 of CKEditor you can configure if you want inline content to be automatically wrapped with tags like <p></p>. This is the correct setting:

从 CKEditor 3.6 版开始,您可以配置是否希望内联内容自动用<p></p>. 这是正确的设置:

CKEDITOR.config.autoParagraph = false;

Source: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.autoParagraph

来源:http: //docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.autoParagraph

回答by Michael Hellein

Across the internet, people have noticed that setting config.enterMode to CKEDITOR.ENTER_BR removes the wrapping paragraph tags from CKEditor. It's worth noting that the setting changes the behavior of the enter key to insert line breaks rather than paragraphs, which is not desirable.

在互联网上,人们注意到将 config.enterMode 设置为 CKEDITOR.ENTER_BR 会从 CKEditor 中删除包装段落标签。值得注意的是,该设置改变了回车键的行为以插入换行符而不是段落,这是不可取的。

See: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.enterMode"It is recommended to use the CKEDITOR.ENTER_P setting because of its semantic value and correctness."

请参阅:http: //docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.enterMode“建议使用 CKEDITOR.ENTER_P 设置,因为它的语义值和正确性。”

However, the setting that is designed to remove that initial paragraph, config.autoParagraph, isn't advisable either, as it introduces "unpredictable usability issues" because the editor reallywants a top-level block element.

但是,旨在删除初始段落 config.autoParagraph 的设置也不可取,因为它引入了“不可预测的可用性问题”,因为编辑器确实需要顶级块元素。

See: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.autoParagraph

请参阅:http: //docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.autoParagraph

The magic happens on wysiwygarea/plugin.js, line 410, where the editor selects the default block element based on config.enterMode. A config option to change the default block element would allow us to start with a div, but we'd continue getting more divs with every enter press, unless we changed the paragraph format via the menu.

神奇发生在 wysiwygarea/plugin.js,第 410 行,其中编辑器根据 config.enterMode 选择默认块元素。更改默认块元素的配置选项将允许我们从一个 div 开始,但每次按下 Enter 时我们都会继续获得更多 div,除非我们通过菜单更改段落格式。

See: http://docs.cksource.com/ckeditor_api/symbols/src/plugins_wysiwygarea_plugin.js.html

请参阅:http: //docs.cksource.com/ckeditor_api/symbols/src/plugins_wysiwygarea_plugin.js.html

It would be possible to remove the wrapping paragraph tag with post-processing (either on the server or in CKEditor's getData event), but that leads us into the same problem as disabling autoParagraph: there's no top-level block.

可以通过后处理(在服务器上或在 CKEditor 的 getData 事件中)删除包装段落标记,但这会导致我们遇到与禁用 autoParagraph 相同的问题:没有顶级块。

I would rather say that there's not a good solution, but rather a handful of half-solutions, than to accept changing config.enterMode as the canonical solution.

我宁愿说没有一个好的解决方案,而是一些半解决方案,而不是接受更改 config.enterMode 作为规范解决方案。

回答by John W

Try this in config.js

在 config.js 中试试这个

CKEDITOR.editorConfig = function( config )
{
config.enterMode = CKEDITOR.ENTER_BR;
config.shiftEnterMode = CKEDITOR.ENTER_BR;
};

回答by PHPGuy

Found it!

找到了!

ckeditor.js line #91 ... search for

ckeditor.js 第 91 行 ... 搜索

B.config.enterMode==3?'div':'p'

B.config.enterMode==3?'div':'p'

change to

改成

B.config.enterMode==3?'div':''(NO P!)

B.config.enterMode==3?'div':''(没有P!)

Dump your cache and BAM!

转储您的缓存和 BAM!

回答by Timothy Nwanwene

MAKE THIS YOUR config.js file code

将此作为您的 config.js 文件代码

CKEDITOR.editorConfig = function( config ) {

   //   config.enterMode = 2; //disabled <p> completely
        config.enterMode = CKEDITOR.ENTER_BR; // pressing the ENTER KEY input <br/>
        config.shiftEnterMode = CKEDITOR.ENTER_P; //pressing the SHIFT + ENTER KEYS input <p>
        config.autoParagraph = false; // stops automatic insertion of <p> on focus
    };

回答by derwiki

I'm doing something I'm not proud of as workaround. In my Python servlet that actually saves to the database, I do:

我正在做一些我不引以为豪的解决方法。在实际保存到数据库的 Python servlet 中,我执行以下操作:

if description.startswith('<p>') and description.endswith('</p>'):
    description = description[3:-4]

回答by Lars

if (substr_count($this->content,'<p>') == 1)
{
  $this->content = preg_replace('/<\/?p>/i', '', $this->content);
}

回答by chim

Edit the source (or turn off rich text) and replace the p tag with a div. Then style the div any which way you want.

编辑源(或关闭富文本)并将 p 标记替换为 div。然后以您想要的任何方式设置 div 样式。

ckEditor won't add any wrapper element on the next submit as you've got the div in there.

ckEditor 不会在下一次提交时添加任何包装元素,因为您已经在那里获得了 div。

(This solved my issue, I'm using Drupal and need small snippets of html which the editor always added the extra, but the rest of the time I want the wrapping p tag).

(这解决了我的问题,我正在使用 Drupal 并且需要编辑器总是添加额外的 html 小片段,但其余时间我想要包装 p 标签)。

回答by Timothy Nwanwene

MAKE THIS YOUR config.js file code

将此作为您的 config.js 文件代码

CKEDITOR.editorConfig = function( config ) {

   //   config.enterMode = 2; //disabled <p> completely
        config.enterMode = CKEDITOR.ENTER_BR // pressing the ENTER KEY input <br/>
        config.shiftEnterMode = CKEDITOR.ENTER_P; //pressing the SHIFT + ENTER KEYS input <p>
        config.autoParagraph = false; // stops automatic insertion of <p> on focus
    };

回答by Evan Ross

Set such config:

设置这样的配置:

    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR
    CKEDITOR.config.forcePasteAsPlainText = true