Jquery:单击突出显示/取消突出显示表行

时间:2022-09-20 09:48:49

I want my script to highlight the row that I select and it works great, but when a row is selected/highlighted i want it to be "deselected/dehighlighted" if another row is selected. How do i do this?

我希望我的脚本突出显示我选择的行并且效果很好,但是当选择/突出显示行时,如果选择了另一行,我希望它“取消选择/取消选择”。我该怎么做呢?

Here is current code for selecting a row, it deselects, but only if i click on the same row again:

这是用于选择行的当前代码,它取消选择,但仅当我再次单击同一行时:

$(".candidateNameTD").click(function() {
            $(this).parents("tr").toggleClass("diffColor", this.clicked);
        });

Html table

Html表

<table id="newCandidatesTable">
    <thead>
        <tr>
            <th style="cursor: pointer;">ID</th>
            <th style="cursor: pointer;">Navn</th>
            <th style="cursor: pointer;">Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    <% foreach (var candidate in Model.Ansogninger)
    {
         %>
            <tr id="<%= candidate.AnsogerID %>" class="newCandidatesTableTr">
                <td><div id="candidateID"><%= candidate.AnsogerID %></div></td>
                <td><div id="<%= "candidateName_" + candidate.AnsogerID %>" class="candidateNameTD"><%= candidate.Navn %></div></td>
                <td><div id="candidateEmail"><%= candidate.Email %></div></td>
                <td>
                    <div id="<%= "acceptCandidateButton_" + candidate.AnsogerID %>" class="acceptb" style="cursor: pointer; border: 1px solid black; width: 150px; text-align: center;">Godkend</div>
                </td>
            </tr>
         <%
    } %>
    </tbody>
    </table>

4 个解决方案

#1


22  

You could first deselect all rows, like

您可以先取消选择所有行,例如

    $(".candidateNameTD").click(function() {
        $(this).closest("tr").siblings().removeClass("diffColor");
        $(this).parents("tr").toggleClass("diffColor", this.clicked);
    });

edit: yep, sry, but the idea was right ;)

编辑:是的,sry,但想法是对的;)

#2


6  

$(".candidateNameTD").click(function() {
            $("tr").removeClass("diffColor");
            $(this).parents("tr").addClass("diffColor");
        });

#3


4  

This will only affect the current table:

这只会影响当前表:

$(".candidateNameTD").click(function() {
    $('table#newCandidatesTable tr').removeClass("diffColor");
    $(this).parents("tr").addClass("diffColor");
});

#4


0  

Best using .live. One event is preferred over many (think big table, big overhead).

最好使用.live。一个事件比许多事件更受欢迎(想想大表,大开销)。

$("div.candidateNameTD").live('click'. function() {
    var $tr = $(this).closest("tr");
    //remove any selected siblings 
    $tr.siblings().removeClass('diffColor');
    //toggle current row
    $tr.toggleClass('diffColor');         
});

#1


22  

You could first deselect all rows, like

您可以先取消选择所有行,例如

    $(".candidateNameTD").click(function() {
        $(this).closest("tr").siblings().removeClass("diffColor");
        $(this).parents("tr").toggleClass("diffColor", this.clicked);
    });

edit: yep, sry, but the idea was right ;)

编辑:是的,sry,但想法是对的;)

#2


6  

$(".candidateNameTD").click(function() {
            $("tr").removeClass("diffColor");
            $(this).parents("tr").addClass("diffColor");
        });

#3


4  

This will only affect the current table:

这只会影响当前表:

$(".candidateNameTD").click(function() {
    $('table#newCandidatesTable tr').removeClass("diffColor");
    $(this).parents("tr").addClass("diffColor");
});

#4


0  

Best using .live. One event is preferred over many (think big table, big overhead).

最好使用.live。一个事件比许多事件更受欢迎(想想大表,大开销)。

$("div.candidateNameTD").live('click'. function() {
    var $tr = $(this).closest("tr");
    //remove any selected siblings 
    $tr.siblings().removeClass('diffColor');
    //toggle current row
    $tr.toggleClass('diffColor');         
});