每个()div有两个类,如果这个div有子节点,则添加css

时间:2015-11-13 16:54:56

标签: javascript jquery html css

我不明白我错在哪里....

<div class="gsc-webResult gsc-result">Ciao</div>
<div class="gsc-webResult gsc-result">
    <td class="gsc-table-cell-thumbnail gsc-thumbnail">Ciao2</td>
</div>


$(".gsc-webResult .gsc-result").each(function () {
   if ($(this).children('td').not('.gsc-table-cell-thumbnail gsc-thumbnail')) {

     $(this).css('margin-left', '60px');
   }
 });

我想添加&#39; margin-left&#39;标记为td的div与这些类(.gsc-table-cell-thumbnail gsc-thumbnail)作为子项

链接jsfiddle: http://jsfiddle.net/s0b75ekw/

感谢

1 个答案:

答案 0 :(得分:2)

安静的混乱代码

<div class="gsc-webResult gsc-result">Ciao</div>
<div class="gsc-webResult gsc-result">
    <span class="gsc-table-cell-thumbnail gsc-thumbnail">Ciao2</span>
</div>

JS

$(".gsc-webResult.gsc-result").each(function () {
//               ^ Remove the space from here Otherwise it will be nested classes 

   // Calculate the number of child this element has with tag td and specified classes.
   if ($(this).find('span.gsc-table-cell-thumbnail.gsc-thumbnail').length>0)    
   {
     $(this).css('margin-left', '60px');
   }
});