jquery,更改父td的css类

时间:2013-01-05 10:10:52

标签: jquery jquery-ui jquery-plugins fullcalendar

我的桌子看起来像这样:

<table class="content">
<tbody>
   <tr class="fc-first">

      <td class="fc-widget-content fc-day0 fc-first">
    <div>
        <div class="fc-day-number">1</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
    </div>
      </td>
      <td class="fc-widget-content fc-day1">
    <div>
        <div class="fc-day-number">2</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
    </div>
      </td>
      <td class="fc-widget-content fc-day2">
    <div>
        <div class="fc-day-number">3</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
    </div>
      </td>
   </tr>
   <tr class="fc-week1"></tr>
   <tr class="fc-week2"></tr>
</tbody>
</table>

我想基于到目前为止仅包含td我的jquery代码的内容将CSS类添加到<div> class"fc-day-number">2</div>

var content_td;
var content = $('.content' );
content_td = content.find('td');

我以为我可以使用嵌套查找,但不知道如何。

2 个答案:

答案 0 :(得分:2)

您需要的是.closest()方法

$('table.content td[class="fc-day-number"]').closest('td').addClass('SOME_CLASS');

和您的固定HTML

<table class="content">
<tbody>

   <tr class="fc-first">

    <td class="fc-widget-content fc-day0 fc-first">
      <div>
        <div class="fc-day-number">1</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
      </div>
    </td>
    <td class="fc-widget-content fc-day1">
      <div>
        <div class="fc-day-number">2</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
      </div>
    </td>
    <td class="fc-widget-content fc-day2">
      <div>
        <div class="fc-day-number">3</div>
        <div class="fc-day-content">
            <div style="position:relative"> </div>
        </div>
      </div>
    </td>

   </tr>

   <tr class="fc-week1"></tr>
   <tr class="fc-week2"></tr>

</tbody>
</table>

答案 1 :(得分:1)

确保引用类选择器,使其成为String文字。另外,将content_td变为变量。

var content = $(".content"); //Quotes added
var content_td = content.find('td');