2009-06-03 46 views
3

我试图向特定类内的tr中的第一个td添加一个类。但由于某种原因它增加了这个类仅在每个TR如何将类添加到使用jQuery的表内的每个tr中的第一个td

$("#appendTD").find("#gridFormInformation").children(0).children().each(
       function() { 
        if ($("#appendTD").find('tr').filter(function() { return $(this).attr('class') == 'ui-widget-content ui-subtblcell'; }).children("td:eq(0)").addClass("leftbord")); 
       } 
      ); 

但是,当我改变上述通过删除“在第一TR -not第一TD第一TD” TD:当量(0)”它增加了这个类在每个TR每个TD ...那我下面

<td id="appendTD"> 
    <table id="gridFormInformation"> 
    <tbody> 
     <tr><th>1</th><th>2</th><th>3</th></tr> 
     <tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr> 
     <tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr> 
     <tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr> 
    </tbody> 
    </table> 
</td> 

回答

5

它LO对我来说,就像你使用复杂的手动方式来找到你想添加类的tds。我相信使用jQuery选择器来查找你的tds会更容易。

更换

$("#appendTD").find("#gridFormInformation").children(0).children().each(
      function() { 
       if ($("#appendTD").find('tr').filter(function() { return $(this).attr('class') == 'ui-widget-content ui-subtblcell'; }).children("td:eq(0)").addClass("leftbord")); 
      } 
     ); 

$("#gridFormInformation tr.ui-widget-content.ui-subtblcell").find("td:first").addClass("leftbord"); 

这会发现所有的TRS和每个TR找到的第一个TD和类添加到。

+0

正是我一直在寻找的呢!谢谢 :-) – 2011-01-14 05:16:25

2

变化'td:eq(0)做错了吗?

例的标记来'td:first'

2

这种方法是将表gridFormInformation的每一行中添加leftbord到第一<td>

$('#gridFormInformation tr td:first-child').addClass('leftbord'); 
相关问题