如何删除不带标签的html文本? 删除:
<div>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
</div>
答案 0 :(得分:1)
元素节点是nodeType
的1个节点,因此您可以遍历所有childNodes
和.remove()
那些不具有.nodeType
的{{1}}的节点只保留元素:
1
const div = document.querySelector('div');
[...div.childNodes].forEach((node) => {
if (node.nodeType !== 1) {
node.remove();
}
});
不太优雅,但是您也可以创建<div>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
***remove***
<p>A car is a wheeled, self-powered motor vehicle used for transportation.</p>
</div>
的数组来获取所有元素子元素,然后清除容器并再次附加所有元素子元素:
.children
const div = document.querySelector('div');
const children = [...div.children];
div.textContent = '';
children.forEach(child => div.appendChild(child));
答案 1 :(得分:-1)
这只是针对您的情况的简单解决方案。
var content = $("div").find("p");
$("div").empty().append(content);