2010-07-19 64 views
1

例如:jQuery的刪除與此鏈接後一定標籤和整個文本鏈接

<a href="/" title="Go to homepage">Homepage</a> text after link; 
<a href="/" title="About">About</a> text after link; 
<a href="/" title="Contact Us">Contact Us</a> text after link; 

沒有問題:我可以刪除該鏈接:

$("a:contains('Homepage')").remove(); 

我的問題:如何去除鏈接後的文本以前刪除:

text after link

在此先感謝。

回答

3

您可以設置文本節點的值清空,就像這樣:

$("a:contains('Homepage')")[0].nextSibling.nodeValue = ""; 

You can try it here。如果您不確定它是否存在,請添加if支票,如下所示:

var node = $("a:contains('Homepage')")[0]; 
if(node && node.nextSibling) node.nextSibling.nodeValue = ""; 
+0

令人驚訝的是,它的工作原理。謝謝尼克。 – josoroma 2010-07-19 02:54:35

+0

@josoroma - 歡迎:) – 2010-07-19 02:55:45