简体   繁体   中英

Using jQuery to Highlight Selected ASP.NET DataGrid Row

It is easy to highlight a selected datagrid row, by for example using toggleClass in the tr's click event. But how best to later remove the highlight after a different row has been selected? Iterating over all the rows to unhighlight them could become expensive for larger datagrids. I'd be interested in the simplest solution, as well as the most performant.

Thanks,
Mike

如果您只想查找具有toggledClass的项目并使用jQuery将其关闭:

$('.toggledClass').removeClass('toggledClass');

This method stores the active row into a variable. The $ at the start of the variable is just my own hungarian notation for jQuery objects.

var $activeRow;

$('#myGrid tr').click(function() {
    if ($activeRow) $activeRow.removeClass('active');
    $activeRow = $(this).addClass('active');
});

为了提高性能,您可以将所选元素的ID推送到var(或多个数组),然后在关闭类时使用该数组的var / iterate。

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