2011-02-24 29 views
2

我需要刪除所有我的文檔中,與「/ {tag_」或「{tag_」jQuery的href屬性開始/ {tag_

到目前爲止,我$("a[href^='/{tag_']").remove();但它不工作,

啓動鏈接

我也有

$("a").each(function() { 
       var href = $(this).attr("href"); 
       if(href == '') { // or anything else you want to remove... 
        $(this).remove(); 

       } 
       $("a[href^='/{tag_']").remove(); 
      }); 

我試圖$(this).attr("href^='/{tag_'");也沒有工作,任何想法?

感謝塔拉

+0

它爲我的jsfiddle - http://jsfiddle.net/yQypx/你不是真的想要做的$(「一[^ HREF =」/{標籤_']「)除去();在每個循環內。 – 2011-02-24 09:03:35

回答

3
$('a').each(function() { 
    $("a[href^='/{tag_']").remove(); 
}); 

這對我的作品:http://jsfiddle.net/neuroflux/tKapr/1/

+2

循環有點多餘,不是嗎?無論如何,remove()對整套選定對象起作用,afaik。 – Flo 2011-02-24 09:16:50

+0

確實如此,但包含它是因爲OP有問題。 (+1給你) – 2011-02-24 09:18:24

+0

乾杯塔拉!!! – 2011-02-28 15:45:28

0
<script src="jquery.js"></script> 
<script type="text/javascript"> 
    $(function(){ 
     $('a').each(
      function(){ 
       if($(this).attr('href').match('^tag_')){ 
        $(this).remove(); 
       } 
      } 
     ); 
    }) 
</script> 
<a href="tag_me">tagme</a> 
1

另一種方法是在每個使用使用match

$('a').each(function() 
{ 
    if ($(this).attr('href').match("^/{tag_")) 
    { 
     $(this).remove(); 
    } 
}); 
+0

使用'match()' - +1的好解決方案 – 2011-02-24 09:22:16

2

沒有多大意義。

可以這樣做:

$("a[href^='/{tag_']").remove(); 
$("a[href^='{tag_']").remove(); 
$("a[href='']").remove();