Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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
html/javascript onclick更改列bg颜色_Javascript_Php_Css - Fatal编程技术网

html/javascript onclick更改列bg颜色

html/javascript onclick更改列bg颜色,javascript,php,css,Javascript,Php,Css,我有一个动态创建的表,我有代码更改一个单元格的bg颜色,我想做的是能够将列更改为不同的bg颜色,以便显示列已被选中,以及列中的哪个单元格已被选中,如果一次只选择一个单元格也很好 单击会将单元格背景更改为红色(工作),单击单元格时,我还需要将列更改为粉红色 谢谢你的帮助 它目前的样子: 我希望它看起来像什么: [ 功能更改颜色(elem){ var cellcolor=elem.style.backgroundColor; 如果(!(cellcolor=='red')){ elem.style

我有一个动态创建的表,我有代码更改一个单元格的bg颜色,我想做的是能够将列更改为不同的bg颜色,以便显示列已被选中,以及列中的哪个单元格已被选中,如果一次只选择一个单元格也很好

单击会将单元格背景更改为红色(工作),单击单元格时,我还需要将列更改为粉红色 谢谢你的帮助

它目前的样子:

我希望它看起来像什么: [


功能更改颜色(elem){
var cellcolor=elem.style.backgroundColor;
如果(!(cellcolor=='red')){
elem.style.backgroundColor=“红色”;
}
否则{
元素风格背景色=”;
}
}
.top1{
背景颜色:黄色;
}
.左{
浮动:左;
文本对齐:居中;
背景色:#fff;
边界半径:25px;
边框:2px实心#8AC007;
左侧填充:5px;
宽度:100px;边距:自动;
}
.对{
浮动:对;
文本对齐:居中;
背景色:#fff;
边界半径:25px;
边框:2px实心#8AC007;
填充:1px;
宽度:25px;边距:自动;
文本对齐:居中;
}
.姓名{
背景颜色:绿色;
}
.坦克{
背景颜色:绿色;
}
.name td:first child{
背景颜色:蓝色;
宽度:180px;
}  
.坦克td:第一个孩子{
背景颜色:蓝色;
宽度:180px;
} 
.tr{
背景颜色:黄色;
}
.坦克{
宽度:20px;
} 
.坦克tr:悬停{
背景色:#aaa;
}
.name tr{
背景颜色:黄色;
宽度:20px;
}
特利斯特先生{
溢出y:滚动;
高度:700px;
顶部:20px;
}   
.球员{
位置:静态;
}  

.jpg“style=”宽度:800px;高度:800px;“alt=”“>
您可以使用jQuery:

  • 首先在head标记中包含jQuery脚本

  • 其次,获取单击事件:

    <script type="text/javascript">
    $(document).ready(function(){
        $('td').click(function(){
           _index = $(this).index();
           $(this).addClass('red');
           $('table th:eq(' + _index + '), table td:eq(' + _index + ')').addClass('pink');
           //This line adds class red to the clicked cell and adds class  pink to column of the clicked cell
       });
    });
    </script>
    
    
    $(文档).ready(函数(){
    $('td')。单击(函数(){
    _index=$(this.index();
    $(this.addClass('red');
    $('table th:eq('+_index+')、table td:eq('+_index+')).addClass('pink');
    //此行将类红色添加到单击的单元格中,并将类粉色添加到单击的单元格的列中
    });
    });
    

  • 您可以使用jQuery进行以下操作:

  • 首先在head标记中包含jQuery脚本

  • 其次,获取单击事件:

    <script type="text/javascript">
    $(document).ready(function(){
        $('td').click(function(){
           _index = $(this).index();
           $(this).addClass('red');
           $('table th:eq(' + _index + '), table td:eq(' + _index + ')').addClass('pink');
           //This line adds class red to the clicked cell and adds class  pink to column of the clicked cell
       });
    });
    </script>
    
    
    $(文档).ready(函数(){
    $('td')。单击(函数(){
    _index=$(this.index();
    $(this.addClass('red');
    $('table th:eq('+_index+')、table td:eq('+_index+')).addClass('pink');
    //此行将类红色添加到单击的单元格中,并将类粉色添加到单击的单元格的列中
    });
    });
    

  • 他只想使列变成粉红色,而不是整个表。$(this).addClass('red').parents('tr').addClass('pink');或$(this.addClass('red').parents().addClass('pink'));他希望collumn是粉红色的,而不是行。我尝试了Viktor上面的代码,但它没有任何作用。我需要什么版本的jqury,因为我的头中包含了1.4.5版,所有这些都已经准备好了使用最新版本的jquery,接下来在td-$('td,th')旁边添加th。单击(function(){他只想使列变成粉红色,而不是整个表。$(此).addClass('red')。parents('tr')。addClass('pink');或$(this)。addClass('red')。parents()。addClass('pink');他希望collumn是粉红色的,而不是行。我用Viktor尝试了上面的代码,但它没有任何作用。我需要什么版本的jqury,因为我在头中包含了版本1.4.5都已经使用最新版本的jquery,接下来在td旁边添加th-$('td,th')。单击(函数(){