繁体   English   中英

删除超链接但保留文本?

[英]Remove hyperlink but keep text?

<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>

每当超链接的标题为“显示个人资料”时,我想删除超链接并仅用文本替换它。

所以代替

<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>

我只想拥有Mentalist

知道如何解决这个问题吗?

这应该工作:

$('a[title="Show Profile"]').contents().unwrap();

这里, 小提琴与证明。

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

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

这样做:

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

它应该只替换第一个链接。

删除超链接但保留文本的香草 JavaScript 方式(而不是 jQuery):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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