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

如何在React的TinyMCE富文本编辑器中添加内部样式

问题描述

我在React应用中使用TinyMCE编辑器,想要实现一个可编辑带有大量flexbox内部样式的HTML页面的编辑器。但该编辑器会移除所有内部样式,迫使我只能使用内联样式,而我不希望用户提交页面时采用内联样式。另外,我通过initialValue提供HTML模板,且内部样式在普通HTML/CSS/JS模式下可正常生效。

代码示例
export default function App() {
   const editorRef = useRef(null);
   const log = () => {
     if (editorRef.current) {
       console.log(editorRef.current.getContent());
     }
   };
   return (
     <>
       <Editor
         onInit={(evt, editor) => editorRef.current = editor}
         initialValue={htmlTemplate}
         init={{
           height: 500,
           menubar: false,
           extended_valid_elements:
                'style[*],div[class|contenteditable|ref|data|style]',
           plugins: [
             'advlist autolink lists link image charmap print preview anchor',
             'searchreplace visualblocks code fullscreen',
             'insertdatetime media table paste code help wordcount'
           ],
           toolbar: 'undo redo | formatselect | ' +
           'bold italic backcolor | alignleft aligncenter ' +
           'alignright alignjustify | bullist numlist outdent indent | ' +
           'removeformat | help',
           content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }'
         }}
       />
       <button onClick={log}>Log editor content</button>
     </>
   );
 }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 14:01:02