You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用TinyMCE v6换行后自动添加<br>标签?

问题描述

我正在使用TinyMCE v6,调用tinymce.get("html").getContent()时,系统总会在换行后自动添加<br>标签。我尝试过配置:

forced_root_block: false,
force_br_newlines: false,
force_p_newlines: false,

但这些配置在v6版本中无效。我的完整初始化代码如下:

tinymce.init({
    selector: 'textarea#html',
    plugins: 'anchor autolink charmap codesample emoticons image media code link lists searchreplace table visualblocks wordcount',
    imagetools_cors_hosts: ['picsum.photos'],
    menubar: 'file edit view insert format tools table help',
    toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough highlight custom | link image media code ruby table mergetags | align lineheight | tinycomments | numlist bullist indent outdent | emoticons charmap removeformat',
    toolbar_sticky: true,
    toolbar_mode: 'wrap',
    autosave_ask_before_unload: true,
    paste_data_images: true,
    autosave_interval: '30s',
    autosave_retention: '2m',
    height: 800,
    statusbar: false,
    forced_root_block: false,
    force_br_newlines: false,
    force_p_newlines: false,
    extended_valid_elements: 'script[language|type|src]',
    content_css: ['/dist/css/adminlte.min.css'],
    content_css_cors: true,
    importcss_append: true,
    convert_urls: false
});
解决方案

TinyMCE v6已移除force_br_newlines、force_p_newlines等旧配置,改用newline_behavior和相关选项控制换行行为。要禁用自动添加<br>标签,按以下方式调整配置:

  • 设置newline_behavior: "linebreak":按下Enter键时插入原生换行符(\n),而非<br>或<p>标签。
  • 保留forced_root_block: false:避免编辑器自动给内容包裹根块元素。
  • 可选:配置content_formatting优化输出:移除不必要的<br>标签,同时保留原生换行。

调整后的核心配置代码:

tinymce.init({
    // 其他原有配置...
    forced_root_block: false,
    newline_behavior: "linebreak",
    content_formatting: {
        remove_trailing_brs: true,
        remove_line_breaks: false
    }
});

如果仅需在获取内容时去除<br>但保留其他HTML标签,也可以在调用getContent()时指定参数,但这种方式仅影响输出结果,不改变编辑器内部行为:

// 获取无<br>标签的HTML内容
const content = tinymce.get("html").getContent({
    format: "html",
    remove_line_breaks: true
});

内容的提问来源于stack exchange,提问作者hoang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 03:38:16