Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 - Fatal编程技术网

Javascript 删除表中的多个选定类

Javascript 删除表中的多个选定类,javascript,jquery,Javascript,Jquery,HTML <table id="flex_home" style="" cellpadding="0" cellspacing="0" border="0"> <tbody> <tr class="trSelected"><td align="center" style="display: none;"><div style="text-align: center; width: 15px;"></td></tr&g

HTML

<table id="flex_home" style="" cellpadding="0" cellspacing="0" border="0">

<tbody>
<tr class="trSelected"><td align="center" style="display: none;"><div style="text-align: center; width: 15px;"></td></tr>
<tr class="trSelected"><td align="center" style="display: none;"><div style="text-align: center; width: 15px;"></td></tr>
<tr class="trSelected"><td align="center" style="display: none;"><div style="text-align: center; width: 15px;"></td></tr>
<tr><td align="center" style="display: none;"><div style="text-align: center; width: 15px;"></td></tr>

</tbody>
</table>
您需要使用:

$('table > tbody > tr').click(function(){
 $('.trselected').removeClass('trselected');
 $(this).addClass('trselected');
});

如果屏幕上有多个表,并且希望允许在每个表中选择一行,则需要仅从当前表中的行中删除类:

$('table > tbody > tr').click(function(){
    $(this).siblings().removeClass('trselected');
    $(this).addClass('trselected');
});

您可以在一行中使用此选项

  $('table > tbody > tr').click(function(){
  $('table > tbody >  tr').
   removeClass('trselected').filter(this).addClass("trselected");
  });
工作演示:

$('table>tbody>tr')。单击(函数(){
$('table>tbody>tr').removeClass('trselected').filter(this).addClass(“trselected”);
});
.trselected{color:red;}

aaa
aaa
aaaa
aaa
像这样试试

$('table > tbody > tr').click(function(){
$('table > tbody > tr').removeClass('trselected');
$(this).addClass('trselected');
});

@user2598136:很高兴它有帮助:)
$('table > tbody > tr').click(function(){
$('table > tbody > tr').removeClass('trselected');
$(this).addClass('trselected');
});