简体   繁体   中英

Can you check if $(this).addClass('selected') has already been added in jquery

I have the following code:

    $("#UpdateTable tr").click(function ()
{
    $(this).addClass('selected').siblings().removeClass('selected');
});

It highlights a row in a table when that row is selected. Is there a way to check to see if the row is already selected or is the currently selected row?

I am trying to be able to select a row and have it be highlighted, then select that same row again and have the highlighting go away. But I'm new to jquery and don't know how to do the check.

$(this).hasClass('selected')

在这里阅读更多关于hasClass()

$("#UpdateTable tr").click(function ()
{
    if($(this).hasClass('selected'))
        $(this).removeClass('selected');
    else
        $(this).addClass('selected').siblings().removeClass('selected');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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