jQuery-OnClick,更改表格单元格的背景颜色 [英] jQuery - OnClick, change background color for table cells

查看:336
本文介绍了jQuery-OnClick,更改表格单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我向您展示一个演示:此处

Let me show you a demo: here

它仅适用于行.它不适用于细胞.我想通过单击鼠标来更改单元格(tds)的背景颜色.

it is working for only rows. its not working for cells. i want to change cells' (tds') background colors with mouse clicks.

例如:a有一个表,它有4个tds.表格的背景颜色为白色.如果单击td,则td应该为红色;如果单击b,b td应该为红色,而td应该再次为白色.如果我单击c,则c现在应该是红色,b应该现在是白色.

For example: a have a table, and it has 4 tds. table's background color is white. if i click to a td, a td should be red, than if i click to b, b td should be red and a td should be white again. if i click to c than, c should be red and b should be white right now.

A-B

C-D

推荐答案

尝试将JavaScript更新为:

Try updating the JavaScript to:

$( function() {
  $('td').click( function() {
    $(this).parents('table').find('td').each( function( index, element ) {
        $(element).removeClass('on');
    } );
    $(this).addClass('on');
  } );
} );

预览

这篇关于jQuery-OnClick,更改表格单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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