jQuery在prev中找到相同的td。 和下一行
我有一张桌子,我知道如何搜索下一个和上一个TD
,但如何在下一个和前一个TR
?
012 345 <- example, I clicked on 4, it checks 3 and 5, but how to check on 0,1,2 and 6,7,8? 678
$(document).ready(function() { $("td").click(function() { var x = $(this).next().css('backgroundColor'); var y = $(this).prev().css('backgroundColor'); if (x == 'rgb(255, 0, 0)' || y == 'rgb(255, 0, 0)') { return; } else { $(this).css('backgroundColor', 'red'); } }); });
我做了一些研究,并提出了一个有效的工作解决方案。 谢谢大家的帮助:
$(this).parent().next().find('td').eq($(this).index()-1).css('backgroundColor');
在td
上使用parent()
方法转到tr
,然后可以相应地使用prev()
或next()
。
$(document).ready(function() { $("td").click(function() { var $tr = $(this).parent();//this will give the tr $tr.prev();//previous tr $tr.next();//next tr //To get the corresponding td's in the next and prev rows var tdIndex = $(this).index(); $tr.prev().find("td:not(:eq(" + tdIndex + "))"); $tr.next().find("td:not(:eq(" + tdIndex + "))"); }); });
像$(this).closest('tr').next('tr').find('td')
会在你的事件中包含
的那个之后获得行内的所有
上。
$(this).parent('tr').next()
和
$(this).parent('tr').prev()
在jquery中检查.parent()
..