2 回答
TA贡献1936条经验 获得超6个赞
您可以尝试下面的代码。
var text = '<div class="class" style="color:#666;font-size:12px"><strong style="font-size:12px;color:#123;font-weight:bold;">XXXXXXX</strong><br /><span>YYYYYYYYYYYYYYYYYY</span></div><div class="classL" style="color:#456;text-align:right;"><a style="color:#789" href="./abc/?id=1">ZZZZZZZ</a></div>'
let parser = new DOMParser()
let _document = parser.parseFromString(text, "text/html")
let _body = _document.getElementsByTagName("body")[0]
let rec = (children) => {
for (let i=0; i<children.length ; i++) {
let child = children[i]
if (child.children.length) {
rec(child.children)
}
else if(child.innerText.length > 0) {
console.log(child.innerText)
}
}
}
rec(_body.children)
- 2 回答
- 0 关注
- 73 浏览
添加回答
举报