简体   繁体   中英

How to exclude the first td when selecting a tr? CSS, JQuery

I have this script that changes background-color for a table's tr when clicked:

<script>
  $('#reviews').on('click', 'tbody tr', function(event) {
    $(this).addClass('highlight').siblings().removeClass('highlight');
  })
</script>

It works fine. However, I'd like to exclude the first td of the tr . So, when the user clicks on the first td nothing happens. How to exclude it?

Tried this:

<script>
  $('#reviews').on('click', 'tbody tr:not(:first-of-type)', function(event) {
    $(this).addClass('highlight').siblings().removeClass('highlight');
  })
</script>

But it still highlight the entire row when clicking on the first td .

You're trying to exclude first tr, but mentioned you wanted first td excluded.

try like this

$('#reviews').on('click', 'tbody > tr', function(event) {
    $(this).parent().find('td:not(:first-of-type)').addClass('highlight');
    $('table#reviews tr > td:not(:first-of-type).highlight').removeClass('highlight');
})

You exclude the first tr not td of each line. If I understand your goals correctly, it should looks something like this:

  $('#reviews').on('click', 'tbody tr td:not( :first-of-type )', function(event) {
    $(this).parent().addClass('highlight').siblings().removeClass('highlight');
  })

Demo link

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