我在Tinymce 6的编辑中有一些内容.我移动了光标并将其放在内容中,然后执行

tinymce.activeEditor.insertContent(`<span class='dummy'>
                        <div><span style="font-style:italic;font-weight:bold;text-decoration:underline;">This Text is BOLD</span></div>
                        <div><span style="font-style:italic;">This text is Italic</span></div>
                        <div><span style="text-decoration:underline;">This Text is&#160; Underlined</span></div>
                      </span>`, {format: 'html'});

当我执行此操作时,div个元素将被删除.

我可以看到BeforeSetContent活动中的内容.但在SetContent事件中,内容被修改,div被删除.有什么办法可以防止这种行为吗?

我试着添加

valid_children: '+div[span], +span[div]' 

在编辑器配置中.

我希望在不修改标签的情况下将html内容添加到tinymce 6编辑器中

推荐答案

The default behavior is to remove certain elements, such as <div>, when they are inserted using the insertContent method.
But you can override this behavior by customizing the editor's schema and adding a rule to allow the <div> element:

tinymce.init({
  selector: '#your-selector',
  // other configurations...
  setup: function (editor) {
    editor.on('BeforeSetContent', function (e) {
      // Allow the <div> element in the content
      e.content = e.content.replace(/<div>/g, '<div data-mce-bogus="1">');
    });
  },
  valid_elements: 'div[*]' // Allow all attributes on <div> elements
});

Javascript相关问答推荐

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

我应该在redux reducer中调用其他reducer函数吗?

通过在页面上滚动来移动滚动条

在Angular中将样式应用于innerHTML

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

在Matter.js中添加从点A到另一个约束的约束

未加载css colored颜色 ,无法将div设置为可见和不可见

当我在Reaction中创建一个输入列表时,我的输入行为异常

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

匹配一个或多个可选重复的特定模式

Reaction useState和useLoaderData的组合使用引发无限循环错误

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

MUI迷你图:如何将$符号添加到MUI迷你图中的工具提示数据

TypeORM QueryBuilder限制联接到一条记录

有角粘桌盒阴影

Played link-Initialize.js永远显示加载符号

如何使pdf.js上的文本呈现为可选?

当S点击按钮时,我如何才能改变它的样式?

暂停后只有一次旋转JS