一个富文本编辑器的需求 需要将原有的DOM结构进行转换 后传递给后端进行解析
以下是一个原本的dom
<div id="div1">
<p style="margin-bottom: 10pt; white-space: normal; text-indent: 30pt; text-align: center;">
<span style="font-family: 仿宋GB2312; font-size: 16pt;">
9月30日,国
<strong>家主席
<span>习近</span>
平同
</strong>
文
<em>莱苏丹哈</em>
桑纳尔
<span style="font-family: 仿宋GB2312; font-size: 16pt; color: rgb(149, 55, 52);">互致贺电,</span>
庆祝中文建交30周年。gfhfhfgh
</span>
</p>
</div>
需要转换的结果
<div id="div1">
<p style="margin-bottom: 10pt; white-space: normal; text-indent: 30pt; text-align: center;">
<span style="font-family: 仿宋GB2312; font-size: 16pt;"> 9月30日,国</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt; font-weight: bold;">家主席</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt; font-weight: bold;">习近</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt; font-weight: bold;"> 平同</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt;"> 文</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt; font-style: italic;"> 莱苏丹哈</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt;">桑纳尔</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt; color: rgb(149, 55, 52);">互致贺电,</span>
<span style="font-family: 仿宋GB2312; font-size: 16pt;"> 庆祝中文建交30周年。gfhfhfgh</span></p>
</div>
- 希望的结果是将所有的dom子节点 都遍历后以《span》包裹 不能是嵌套的 需要扁平化
- 扁平化后的文本需要继承父类的style,如果是《strong》元素则需要添加一个新的style font-weight:bold,
希望可以提供一下思路
递归。childNodes,把所有节点都包装一下,然后 append。text 的话取父级样式,其他取自己样式。