2011-05-31 140 views
28
<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

不知道如何解决?

回答

80

这应该工作:

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

这里有证明一个Fiddle

+1

解包()是相当整洁。为了完整起见,下面是另一个也应该起作用的解决方案:$(“a [title ='Show Profile']”)。replaceWith(function(){return $(this).contents();}); – muffinista 2011-05-31 13:35:02

+0

非常好的解决方案 – AVH 2013-04-17 22:15:30

+1

真的很聪明! +10 – 2016-12-22 14:12:39

2

这样做:

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

应该只替换第一个链接。

+0

DanielB在这里提供了一个更好的解决方案:http://stackoverflow.com/questions/6188277/remove-hyperlink-but-keep-text/6188344#6188344 – rciq 2011-05-31 13:38:19

2

为此对多个类别的链接,

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