jQuery find same td in prev. and next row

北慕城南 提交于 2019-12-22 04:16:24

问题


I have a table and I know how to search next and prev TD, but how to do it on the next and previous 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');
        }
    });
});

回答1:


I made some research and came up with a working solution that works. Thanks all for help:

$(this).parent().next().find('td').eq($(this).index()-1).css('backgroundColor');



回答2:


Use parent() method on td to go to tr and then you can use prev() or next() accordingly.

$(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 + "))");

    });
});



回答3:


Something like $(this).closest('tr').next('tr').find('td') will get you all the <td>s within the row after the one containing the <td> your event fired on.




回答4:


$(this).parent('tr').next()

and

$(this).parent('tr').prev()



回答5:


Try with parent ;) http://api.jquery.com/parent/




回答6:


check out .parent() in jquery..



来源:https://stackoverflow.com/questions/9726111/jquery-find-same-td-in-prev-and-next-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!