我正在处理表格中包含的表单的代码。我正在写(使用jQuery)一个函数来突出显示每个<input>
元素的父代<td>
。这部分很简单 - 代码只是:jQuery找到最高父母TD
$('.myForm input').click(function(){
$(this).parent().addClass('active');
})
更复杂的是,一些文本字段是嵌套在第一台的<td>
内的第二个表内。它看起来像:
<table>
<tr>
<td> <--cell I want to add the class to
<table>
<tr>
<td><input type='text'></td>
</tr>
</table>
</td>
</tr>
</table>
所以我的问题是:有没有使用jQuery的一个语句找出最高父<input>
件<td>
的方法吗?因此,换句话说,我可以结合:
$('.myForm input').click(function(){
$(this).parent().addClass('active');
})
和
$('.myForm input').click(function(){
$(this).parent().parent().addClass('active');
})
成一个功能?
+1比我快:-P –