简体   繁体   English

高光一次仅切换一行,切换CSS类

[英]highglight only one row at a time, toggling css class

if I have rows in a table like this http://output.jsbin.com/icusec/2 where I'm highlighting selected row on a click, how can I improve this to have selected only one, for example if I first select third row it will get highlighted but when I click on second row then second row should become highlighted and third should not. 如果我在http://output.jsbin.com/icusec/2这样的表中有行,则在单击时突出显示选中的行,那么如何改善此状态以仅选择一个,例如,如果我首先选择第三行将突出显示,但是当我单击第二行时,第二行应突出显示,而第三行则不会。

$(document).ready(function(){
    $("#rowClick").children("tbody").children("tr").children("td").click(function(){
        $(this.parentNode).toggleClass("active");
    });
});

table, table tr, table tr td {margin: 0; padding: 0; border: 0; cursor: pointer;}
table tr.active td {background: #ccc;}

<table id="rowClick">
    <tr>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
    </tr>
    <tr>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
    </tr>
    <tr>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
        <td>Cell</td>
    </tr>
  </table>

Your JavaScript would like something like: 您的JavaScript会像这样:

 $(document).ready(function() { $("#rowClick").on('click', 'tr', function() { $(this) .addClass('active') .siblings() .removeClass('active'); }); }); 
 table, table tr, table tr td { margin: 0; padding: 0; border: 0; cursor: pointer; } table tr.active td { background: #ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="rowClick"> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table> 

You can do it like that: 您可以这样做:

 $(document).ready(function(){ $("#rowClick").children("tbody").children("tr").children("td").click(function(){ $("tr").removeClass("active"); $(this.parentNode).addClass("active"); }); }); 
 table, table tr, table tr td {margin: 0; padding: 0; border: 0; cursor: pointer;} table tr.active td {background: #ccc;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="rowClick"> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table> 

 $(document).ready(function() { $("#rowClick").children("tbody").children("tr").children("td").click(function(){ $('#rowClick tbody tr').removeClass('active') $(this.parentNode).toggleClass("active"); }); }); 
 .active{color:red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="rowClick"> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table> 

  1. Remove class on all tr then add the class on clicked parent(add selector to be specific to avoid affecting other tr) 删除所有tr上的类,然后在单击的父级上添加类(添加特定的选择器以避免影响其他tr)

Check this, 检查一下

$(document).ready(function(){
    $("td").click(function(){
        $(this).closest("#rowClick").find("tr").removeClass("active");
        $(this.parentNode).addClass("active");
    });
});

I hope this will work. 我希望这会起作用。

Here is a solution where class can be removed after second click on the same row. 这是一个解决方案,可以在第二次单击同一行后删除类。

 $(document).ready(function(){ $("#rowClick tbody tr").click(function(){ if ($(this).hasClass("active")) { $(this).removeClass("active"); } else { $("#rowClick tbody tr").removeClass("active"); $(this).addClass("active"); } }); }); 
 table, table tr, table tr td {margin: 0; padding: 0; border: 0; cursor: pointer;} table tr.active td {background: #ccc;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="rowClick"> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM