Remove hyperlink but keep text?
<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>
Whenever a hyperlink has a title of "Show Profile" I want to remove the hyperlink and replace it with only with the text.
So ins开发者_如何学编程tead of
<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>
I want to have only Mentalist
.
Any idea how to solve that?
this should work:
$('a[title="Show Profile"]').contents().unwrap();
Here a Fiddle with the proof.
Vanilla JavaScript way (instead of jQuery) to remove hyperlink but keep text:
const links = document.querySelectorAll('a[title="Show Profile"]')
links.forEach(link => {
const el = document.createElement('span')
el.textContent = link.textContent
link.parentNode.replaceChild(el, link)
})
This will do:
<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>
<a href="http://www.website.com/something" title="Something Else">Mentalist</a>
<script type="text/javascript">
$("a[title='Show Profile']").each(function(){
$(this).replaceWith($(this).text());
});
</script>
It should replace only the first link.
To do this on links of multiple classes,
$("a.className1, a.className2").contents().unwrap();
精彩评论