How do I change HTML table cell color on click

前端 未结 3 1471
野趣味
野趣味 2020-12-21 00:48

I\'m trying to change the background color of an HTML table cell when the user clicks on the cell. Any ideas on how to do this? I have access to the JS Prototype library, so

3条回答
  •  [愿得一人]
    2020-12-21 01:13

    You could loop over all the children of the table and add an click event to them

    with prototype the code is:

    $('your_table').observe('click', function(event) {
      var clickedCell = event.findElement('td');
      if (clickedCell) {
       clickedCell.setStyle({ background: '#dfd' });
      }
    });
    

提交回复
热议问题