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

如何阻止TinyMCE自动添加闭合标签、引号及强制HTML缩进?

Got it, let's fix those TinyMCE auto-formatting annoyances you're dealing with—all of these behaviors can be adjusted via the editor's initialization config. Here's a step-by-step breakdown:

1. Stop TinyMCE from auto-closing <li> tags

By default, TinyMCE enforces valid HTML by closing unclosed elements. To allow unclosed <li> tags, tweak two key settings:

  • Use allow_unclosed_elements to specify which elements don’t need mandatory closing tags
  • Adjust valid_elements to explicitly define allowed elements and their attributes (so the editor doesn’t strip or modify them unexpectedly)

2. Prevent class attributes from getting wrapped in double quotes

TinyMCE automatically normalizes attribute quoting, but you can disable this entirely. Set the quote_attributes option to 'none'—this will keep your class attributes unquoted exactly as you input them.

3. Disable forced HTML indentation

Turn off automatic indentation by setting indent to false. This stops the editor from adding extra whitespace or indentation to your HTML structure against your wishes.

Full Configuration Example

Here’s how to bundle all these fixes into your TinyMCE initialization code:

tinymce.init({
  selector: '#your-editor-container',
  // Allow unclosed <li> tags
  allow_unclosed_elements: 'li',
  // Define valid elements and their attributes (customize as needed)
  valid_elements: 'ul,li[class]',
  // Skip wrapping attributes in quotes
  quote_attributes: 'none',
  // Disable automatic HTML indentation
  indent: false,
  // Preserve your original line breaks
  preserve_newlines: true,
  // Turn off strict HTML validation that overrides your settings
  verify_html: false
});

A quick heads-up: Disabling some HTML validation features means you’ll be working with non-standard HTML, so double-check that your front-end can render this structure correctly. But if that’s exactly the behavior you need, this config should resolve all your issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:32:40