jQuery选择器用于获取同一列中的单元格 [英] jQuery selector to grab cells in the same column

查看:129
本文介绍了jQuery选择器用于获取同一列中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个多行多列表,我如何选择同一中的所有单元格与任意单元格(例如单击的单元格)。

Given a multirow, multicolumn table, how can I select all cells in the same column as any arbitrary cell (e.g. a cell that is clicked on).

类似于:

$("td").click(function(){
    var columnNo = $(this).columnNo?
    $(this).closest("table").find("tr td:eq("+columnNo+")").css("color", "red");
});

我需要在不单独命名列的情况下执行此操作。例如。它应该在简单的通用标记上工作而不需要额外的类或ID ..

I need to do this without naming the columns individually. E.g. it should work on simple generic table markup without extra classes or IDs..

推荐答案

您的尝试是正确的,您需要做的就是使用 .index 查找行中的列号 - < td> 的索引。您还需要使用 nth-child-selector 来匹配其他列的索引< td> 元素。

Your attempt is right, all you need to do is use .index to find the column number—the index of the <td> within the row. You also need to use the nth-child-selector to match the column indices of the other <td> elements.

$("td").click(function(){
    var columnNo = $(this).index();
    $(this).closest("table")
        .find("tr td:nth-child(" + (columnNo+1) + ")")
        .css("color", "red");
});

这篇关于jQuery选择器用于获取同一列中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆