2011-02-24 45 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();