格式化html源码正则,如何格式化富文本编辑器的代码内容?

用楼上思想实现的:

const html = `

文字1

文字2 

文字3 

文字4

let div = document.createElement('div');

div.innerHTML = html;

function getData(node, data) {

if (!Array.isArray(data)) {

throw TypeError('data is not Array');

}

if (node.hasChildNodes()) {

node.childNodes.forEach(element => {

if (element.nodeType === 1) { // 元素结点

getData(element, data); // 递归

}

if (element.nodeType === 3) { // 文本结点

const text = element.nodeValue;

if (!text.match(/^\s*$/g)) { // 非空白字符

const style = element.parentNode.style; // 父节点样式

data.push({

text: text.trim(),

color: style.color,

font_size: style.fontSize,

});

}

}

});

}

}

let data = [];

getData(div, data);

console.log(data);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值