我的桌子看起来像这样:
<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');
我以为我可以使用嵌套查找,但不知道如何。
答案 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');