简体   繁体   中英

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 instead 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.

要在多个类的链接上执行此操作,

$("a.className1, a.className2").contents().unwrap();

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.

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)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM