如何通过单击一行中的按钮来更改html表的一行的颜色

How to change color of a row of html table by click a button in that row?

本文关键字:一行 颜色 html 何通过 单击 按钮      更新时间:2023-09-26

这是我的表

echo"<table id='"company'">";
                while($row=mysql_fetch_array($res))
                    {
                         $s=  $row['company_id'];
                         $r= $row['company_name'];
                         $a= $row['head_office_city'];
                        echo"<tr><td>".$r."</td><td>".$a."</td>
                        <td><a href='"#'" class='"topopup'" id='"update_comp'" onClick='"callYourPopup(".$row['company_id'].");'"><img title='update details' alt='"Delete'" class='del' src='images/update.png'/></a> </td>
                        <td><a href='"company_inactive.php?column1=".$row['company_id']."'"><img title='make company inactive' alt='"Delete'" class='del' src='images/delete.png'/></a> </td></tr>";
                    }
                    echo"</table>";
                    }

我想改变颜色时,我点击在图像的最后一列。实现这一目标最简单的方法是什么?

$('#company td a img').click(function(){
   $(this).closest('tr').addClass('changeColor');
});

,然后使用。changecolor td{background:red}来改变该行的颜色

最简单的方法是为每个图像添加onclick处理程序:

onclick="this.parentNode.parentNode.style.backgroundColor='red'"

但是我不推荐这样写代码。

如果你已经包含jQuery到项目中,那么使用$('#tableID tr td:last img').click(changeRowColor)。式中changeRowColor

function () {
  $(this).closest('tr').css('background-color', 'red');
}

function () {
  $(this).closest('tr').addClass('colorized');
}