Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击即可为表列着色_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 单击即可为表列着色

Javascript 单击即可为表列着色,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用tablesorter()jquery插件对表数据进行排序。。。有人知道是否可以在单击时更改整个列的颜色吗?即使没有这个插件,还有其他方法吗 谢谢, Mike不熟悉此插件,但假设每列上都有一个类,您可以在单击时执行以下操作: $("#table .column-class").addClass("highlighted"); 请注意,您将向每个中的每个添加一个类,而当您高亮显示一行时,您只需向一行添加一个类即可。然后在CSS中制定如下规则: #table .column-class

我正在使用tablesorter()jquery插件对表数据进行排序。。。有人知道是否可以在单击时更改整个列的颜色吗?即使没有这个插件,还有其他方法吗

谢谢,
Mike不熟悉此插件,但假设每列上都有一个类,您可以在单击时执行以下操作:

$("#table .column-class").addClass("highlighted");
请注意,您将向每个中的每个添加一个类,而当您高亮显示一行时,您只需向一行添加一个类即可。然后在CSS中制定如下规则:

#table .column-class {
    background-color:;
}

您可以动态地找出click列的类,然后在选择器中使用该类,以使其更易于重用。希望这是有道理的。我还要快速查看文档,以确保这不是已经支持的插件。

不熟悉此插件,但假设每列上都有一个类,您可以在单击时执行以下操作:

$("#table .column-class").addClass("highlighted");
请注意,您将向每个中的每个添加一个类,而当您高亮显示一行时,您只需向一行添加一个类即可。然后在CSS中制定如下规则:

#table .column-class {
    background-color:;
}

您可以动态地找出click列的类,然后在选择器中使用该类,以使其更易于重用。希望这是有道理的。我还要快速查看文档,以确保这不是已经支持的内容。

查看链接到的文档,它们提到了排序开始/停止时触发的几个触发器。通过将它们绑定到表来连接它们

var table=$('#myTable').tablesorter();
table.bind('sortEnd', updateCells);
查看他们在示例中使用的代码,我发现排序的标题有一个类“headerSortUp”或“headerSortDown”。从这里,我们找出哪个
具有这些类之一,并突出显示其列单元格

function updateCells(){
  var sortHead=$('.headerSortUp, .headerSortDown', table).get()[0],
  index=$('th', table).index(sortHead);

  if (index>=0){
    $('td', table).removeClass('selected');
    $('tr', table).each(function(){
     $('td:eq('+index+')', this).addClass('selected');

    });

  }
}

查看您链接的文档,它们提到了排序开始/停止时触发的几个触发器。通过将它们绑定到表来连接它们

var table=$('#myTable').tablesorter();
table.bind('sortEnd', updateCells);
查看他们在示例中使用的代码,我发现排序的标题有一个类“headerSortUp”或“headerSortDown”。从这里,我们找出哪个
具有这些类之一,并突出显示其列单元格

function updateCells(){
  var sortHead=$('.headerSortUp, .headerSortDown', table).get()[0],
  index=$('th', table).index(sortHead);

  if (index>=0){
    $('td', table).removeClass('selected');
    $('tr', table).each(function(){
     $('td:eq('+index+')', this).addClass('selected');

    });

  }
}

只要没有嵌套表,此示例将适用于大多数表。即使如此,如果你选择了相应的选择器,你也不会有问题。既然您已经在使用jQuery插件,我想我也可以使用它

$(function(){
    //you might want to be a bit more specific than only 'td', maybe 'table.classname td' or 'table#id td'
    $('td').click(function(){
        var $this = $(this);
        //find the index of the clicked cell in the row
        var index = $this.prevAll().length;
        //go back to the parent table (here you might also want to use the more specific selector as above)
        //and in each row of that table...
        $this.parents('table').find('tr').each(function(){
            //...highlight the indexth cell
            $(this).find('td:eq('+index+')').css('background-color', 'yellow')
        })
    })
})

您可能希望使用toggleClass('highlighted')而不是css('background-color','yellow')

此示例适用于大多数表,只要没有嵌套表。即使如此,如果你选择了相应的选择器,你也不会有问题。既然您已经在使用jQuery插件,我想我也可以使用它

$(function(){
    //you might want to be a bit more specific than only 'td', maybe 'table.classname td' or 'table#id td'
    $('td').click(function(){
        var $this = $(this);
        //find the index of the clicked cell in the row
        var index = $this.prevAll().length;
        //go back to the parent table (here you might also want to use the more specific selector as above)
        //and in each row of that table...
        $this.parents('table').find('tr').each(function(){
            //...highlight the indexth cell
            $(this).find('td:eq('+index+')').css('background-color', 'yellow')
        })
    })
})

您可能希望使用toggleClass('highlighted')来代替css('background-color','yellow')

您可以动态更改css,以获得css速度而不是较慢的DOM

当您单击一列时,即:第三列
为具有特殊背景的“td:nth child(3)”添加带有ID的样式

然后单击第二个按钮。
您将删除具有ID的样式。 然后添加一个带有背景的样式“td:nth child(2)”。
等等

下面是我在应用程序中用于动态添加样式的源代码示例

添加样式 上面的'css'是一个字符串,类似于:td:nth child(2){background:#FEB}

移除它
可能您可以动态更改CSS,以获得CSS速度而不是缓慢的DOM

当您单击一列时,即:第三列
为具有特殊背景的“td:nth child(3)”添加带有ID的样式

然后单击第二个按钮。
您将删除具有ID的样式。 然后添加一个带有背景的样式“td:nth child(2)”。
等等

下面是我在应用程序中用于动态添加样式的源代码示例

添加样式 上面的'css'是一个字符串,类似于:td:nth child(2){background:#FEB}

移除它