jQuery highlighting selected columns only in a table

后端 未结 6 1096
长发绾君心
长发绾君心 2021-01-02 19:46

I see this post on highlighting even columns but can I highlight only selected columns?

Here is the code they use:

$(\"table.Table22 > tbody >          


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 20:36

    If you create a link in your table headers, you can do something like this:

    $("table.tbl th a").click(function() {
       var colnum = $(this).closest("th").prevAll("th").length;
    
       $(this).closest("table").find("tr td").removeClass("highlight");
       $(this).closest("table").find("tr td:eq(" + colnum + ")").addClass("highlight");
    }
    

    That will set all cells below the clicked link to class "highlight".

    Of course, you should still set the correct style in your CSS file:

    table.tbl tr .highlight {  background-color: blue; }
    

提交回复
热议问题