添加

时删除不需要的换行符在CK编辑器中

发布于 2024-08-27 02:51:00 字数 458 浏览 6 评论 0原文

当在 CKEditor 中加载包含一组段落的内容时,它会将我的

标记替换为

	

这意味着编辑器将转换此内容:

<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>

最终结果是这样的:

<p>
   paragraph 1</p>
<p>
   paragraph 2</p>
<p>
   paragraph 3</p>

如何修复它,以便 CKEditor 在看到段落标签时不会添加额外的换行符?

When loading content with a set of paragraphs in CKEditor, it replaces my <p> tags with <p>

That means the editor converts this:

<p>paragraph 1</p>
<p>paragraph 2</p>
<p>paragraph 3</p>

into what ends up like this:

<p>
   paragraph 1</p>
<p>
   paragraph 2</p>
<p>
   paragraph 3</p>

How do I fix it so that CKEditor doesn't add the extra newline characters when it sees the paragraph tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑕疵 2024-09-03 02:51:00

在其他地方(我很抱歉,我没有记下我从哪里得到它。),我找到了解决所有块级标签的这个问题的代码。对于我的项目,由于输出到 XML 并作为 CDATA 导入到其他应用程序,额外的换行符成为一个问题。

因此,在我的 ckeditor_config.js 文件中,在 CKEDITOR.editorConfig 函数之后,我输入了以下内容:

CKEDITOR.on('instanceReady', function( ev ) {
  var blockTags = ['div','h1','h2','h3','h4','h5','h6','p','pre','li','blockquote','ul','ol',
  'table','thead','tbody','tfoot','td','th',];

  for (var i = 0; i < blockTags.length; i++)
  {
     ev.editor.dataProcessor.writer.setRules( blockTags[i], {
        indent : false,
        breakBeforeOpen : true,
        breakAfterOpen : false,
        breakBeforeClose : false,
        breakAfterClose : true
     });
  }
});

其中一些元素可能不需要这种处理;显然,可以根据您的需要轻松编辑 blockTags 数组。

Elsewhere (my apologies that I did not make a note of where I got it from.), I found code to fix this problem for all the block-level tags. For my project, the extra new-lines were a problem due to outputting to XML and importing into other applications as CDATA.

So, in my ckeditor_config.js file, after the CKEDITOR.editorConfig function, I put in this:

CKEDITOR.on('instanceReady', function( ev ) {
  var blockTags = ['div','h1','h2','h3','h4','h5','h6','p','pre','li','blockquote','ul','ol',
  'table','thead','tbody','tfoot','td','th',];

  for (var i = 0; i < blockTags.length; i++)
  {
     ev.editor.dataProcessor.writer.setRules( blockTags[i], {
        indent : false,
        breakBeforeOpen : true,
        breakAfterOpen : false,
        breakBeforeClose : false,
        breakAfterClose : true
     });
  }
});

Some of those elements may not need this treatment; obviously the blockTags array can easily be edited to your needs.

我为君王 2024-09-03 02:51:00

将此行添加到 ckeditor.js 配置文件中:

n.setRules('p',{indent:false,breakAfterOpen:false});

有关 HTML Writer 格式的更多信息,请参阅

Add this line to the ckeditor.js configuration file:

n.setRules('p',{indent:false,breakAfterOpen:false});

More about the formatting of the HTML Writer can be found in Developers Guide: Output Formatting at CKSource Docs.

纵山崖 2024-09-03 02:51:00

我相信有一个设置可以格式化代码,或者自动缩进或者类似的东西。其目的是使源代码更具可读性。我想它的有效性还有待商榷。

I believe there's a setting to format the code, or auto-indent or something along those lines. It was intended to make the source code more readable. It's effectiveness is open to opinion I guess.

怀中猫帐中妖 2024-09-03 02:51:00

如果您像我一样并且想在实例级别执行此操作并且不想触及配置文件,以便轻松更新/升级。那么这是另一个解决方案。

CKEDITOR.replace( 'editor1',
{
    on :
    {
        instanceReady : function( ev )
        {
            // Output paragraphs as <p>Text</p>.
            this.dataProcessor.writer.setRules( 'p',
                {
                    indent : false,
                    breakBeforeOpen : true,
                    breakAfterOpen : false,
                    breakBeforeClose : false,
                    breakAfterClose : true
                });
        }
    }
});

参考:输出格式

If you are like me and would like to do it at instance level and don't want to touch the configuration files so that it is easy to update/upgrade. Then here is another solution.

CKEDITOR.replace( 'editor1',
{
    on :
    {
        instanceReady : function( ev )
        {
            // Output paragraphs as <p>Text</p>.
            this.dataProcessor.writer.setRules( 'p',
                {
                    indent : false,
                    breakBeforeOpen : true,
                    breakAfterOpen : false,
                    breakBeforeClose : false,
                    breakAfterClose : true
                });
        }
    }
});

Reference: Output Formatting

迷雾森÷林ヴ 2024-09-03 02:51:00

像魅力一样工作的最佳解决方案:

编辑 contents.css 文件并设置段落样式,例如

 p {
     margin-top:0px;
     margin-bottom:5px;
 }

Best solution that would work like a charm:

edit contents.css file and setting style for paragraphs e.g.

 p {
     margin-top:0px;
     margin-bottom:5px;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文