如何删除只有<br>标签的h2标签?
<h2><br></h2>
发布于 2020-08-23 00:11:28
您可以使用alterChildren prop删除一些DOM节点:
const html = `
<body>
<h2>Hello world</h2>
<p>Lorem ipsum</p>
<h2><br></h2>
</body>
`;
function hasOnlyBrChildren(node) {
return node.children.every(
(child) => child.type === 'tag' && child.name === 'br'
);
}
function alterChildren(node) {
return node.children.filter(
(child) =>
child.type !== 'tag' ||
!(child.name === 'h2' && hasOnlyBrChildren(child))
);
}
export default function App() {
return (
<ScrollView>
<HTML alterChildren={alterChildren} html={html} />
</ScrollView>
);
}我在这里做了一些小事(我只是用div替换了br,以直观地评估该函数的有效性):https://snack.expo.io/@jsamr/rnrhtml-alter-children
https://stackoverflow.com/questions/63524069
复制相似问题