Javascript 如何从html内容中删除所有链接
假设我有一个html内容,比如Javascript 如何从html内容中删除所有链接,javascript,Javascript,假设我有一个html内容,比如 <p id="p_content"> Test <a href='https://google.com'>google</a> Test <a href='https://example.co'>example</a> Hello <a href='https://Test.com'>test</a> </p> 它只返回一个空对象,不返
<p id="p_content">
Test
<a href='https://google.com'>google</a>
Test
<a href='https://example.co'>example</a>
Hello
<a href='https://Test.com'>test</a>
</p>
它只返回一个空对象,不返回任何值。您可以使用remove()
document.querySelectorAll(“#p#u content a”).forEach(el=>el.remove())
试验
试验
你好
简单:
var links = document.querySelectorAll("a"); // Select your links
links.forEach((link) => { // loop through them
link.parentElement.removeChild(link); // find their parent element, then remove them
});
var links = document.querySelectorAll("a"); // Select your links
links.forEach((link) => { // loop through them
link.parentElement.removeChild(link); // find their parent element, then remove them
});