2012-07-25 143 views
0

我有一些jQuery代码,它选择我的网站中以“http”开头的所有<a>标签,并添加通用图标或该网站的favicon并完美地工作。 我的问题是,有时<a>标签包围的<img>标签,我不希望它选择那些,让我怎么告诉jQuery的是什么?我以某种方式使用:not()jQuery CSS - 不要选择<img>标签<a>标签

下面是当前的jQuery代码:

$("#bodyCopy a[href^='http']").each(function() { 
    $(this).css({ 
     background: "url(http://g.etfv.co/" + this.href + ") left center no-repeat", 
     "padding-left": "20px" 
    }); 
}); 

回答

1
$("#bodyCopy a[href^='http']").each(function() { 
    if ($(this).find("img").length < 1) { // Count <img> children 
     // Continue only if fewer than one <img> within <a> 
     $(this).css({ 
      background: "url(http://g.etfv.co/" + this.href + ") left center no-repeat", 
      "padding-left": "20px" 
     }); 
    } 
}); 
+0

谢谢,非常完美! – stephmoreland 2012-07-25 17:53:35

+0

很高兴我能帮助! – 2012-07-25 17:56:18