如何删除h2
只有标签的<br>
标签?
<h2><br></h2>
你可以使用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>
);
}
我这里做了点心(我只是替换br
为div
直观评估功能的有效性):https ://snack.expo.io/@jsamr/rnrhtml-alter-children