使用嵌套的div将类添加到tr

时间:2012-07-24 14:10:54

标签: jquery

我正在尝试使用jquery将类添加到带有类名.flexme1

的表中的tr。

我尝试了下面的各种选项,但似乎没有工作..

  //jQuery("div.flexigrid > table.flexme1 > tr").addClass("cal");

or

        jQuery("div.flexigrid > table.flexme1 > tr").addClass("sev1");
  or 
        jQuery(".erow").addClass("sev2");
   or
        jQuery(".flexigrid > tr").addClass("sev3");
   or
        jQuery(".flexigrid >tr").addClass("sev4");
   or
        jQuery(".flexigrid >tr").addClass("sev5");

我希望tr拥有class =" somename"添加到它。

<div class="flexigrid">
  ..< some other divs>...<<table> ..</table></div>
... <some other divs>...<table>..</table></div>
---
<div class="bDiv" >
   <table cellspacing="0" cellpadding="0" border="0" class="flexme1" >
      <tbody>
         <tr  class="sev1">
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr class="erow"  class="sev1">
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">1</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr   class="sev3">
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr class="erow" >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">1</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">BM1M</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr class="erow" >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">4</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">BM1N</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr class="erow" >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
         <tr class="erow" >
            <td align="left" abbr="InstanceDisplayName8">
               <div style="text-align: left; width: 190px;">undefined</div>
            </td>
            <td align="right" abbr="severity" class="sorted">
               <div style="text-align: right; width: 40px;">&nbsp;</div>
            </td>
            <td align="right" abbr="Impact">
               <div style="text-align: right; width: 40px;">0</div>
            </td>
            <td align="right" abbr="occurrencecount">
               <div style="text-align: right; width: 40px;">undefined</div>
            </td>
            <td align="left" abbr="NotificationID">
               <div style="text-align: left; width: 100px;">Host</div>
            </td>
            <td align="left" abbr="Name">
               <div style="text-align: left; width: 120px;">Down</div>
            </td>
            <td align="right">
               <div style="text-align: right; width: 130px;">undefined</div>
            </td>
         </tr>
      </tbody>
   </table>
   <div class="iDiv" style="display: none;"></div>
</div>


</div><!-- end of div>

2 个答案:

答案 0 :(得分:1)

你几乎是正确的,只是remove the > - table不是.flexigrid div.bDiv,因为它们之间还有另一个jQuery("div.flexigrid table.flexme1 tr").addClass("somename");

{{1}}

答案 1 :(得分:1)

这样做: -

jQuery(".bDiv table.flexme1 tr").addClass("inside");

参考 LIVE DEMO