如何阻止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_elementsto specify which elements don’t need mandatory closing tags - Adjust
valid_elementsto 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

