Javascript 删除表中的多个选定类
HTMLJavascript 删除表中的多个选定类,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
<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');
});