next()不工作
我有一个表结构:
data data2
现在,在加载时,隐藏第2和第4行。 点击它的下一行
将显示出来。
为此我写了:
$("a.xx").click(function (event) { $(this).next(".cc").toggleClass();// not working });
任何线索?
编辑:
点击第一行的 ,它应该显示第二行的
,点击第三行
,它应该显示第四行的
,一次只显示一个
。
CSS
.cc { display: none; }
编辑:根据进一步说明,您希望再次单击以关闭打开的图像。
做这个:
$(this).closest('tr').next('tr').find("img.cc").toggle() .closest('tr').siblings('tr').find("img.cc").hide();
或者这个,效率更高一点:
$(this).closest('tr').next('tr').find("img.cc").toggle(0, function() { var $th = $(this); if( $th.is(':visible') ) $th.closest('tr').siblings('tr').find("img.cc").hide(); });
编辑:根据澄清,似乎你想在下一行显示图像,并隐藏其余部分。
做这个:
$(this).closest('tr').next('tr').find("img.cc").show() .closest('tr').siblings('tr').find("img.cc").hide();
原始答案:
做这个:
$(this).closest('tr').next('tr').find("img.cc").toggleClass('someClass');
jQuery的.next()
只查看元素的兄弟 。
您需要遍历.closest()
元素,获取.next()
行,然后.find()
. .find()
元素。
我还假设您将类名传递给.toggleClass()
而不是在没有参数的情况下调用它。
否则,要显示您可能会使用
.show()
。