【问题标题】:how to change td background color after click点击后如何更改td背景颜色
【发布时间】:2017-07-02 12:36:51
【问题描述】:
我想向用户显示消息或任何类型的通知,例如单击后更改绿色背景颜色或“您单击成功”之类的消息。我的网站图片截图:
这是我的代码:
<tbody>
<tr>
<td>
<?php
$url=$res['url'];
$id=$res['id'];
?>
<a href="out.php?id=<?php echo $id;?>" onclick="myFunction()" target="_blank" onfocus="curTab">
<img src="admin/uploads/<?php echo $res['image']; ?>" id="click"style="width:200px;height:75px;" />
</a>
</td>
</tr>
</tbody>
【问题讨论】:
标签:
javascript
jquery
html
css
【解决方案1】:
您应该使用 Javascript/jQuery 进行客户端通知。
jQuery 解决方案:
<tr>
<td>
<?php
$url=$res['url'];
$id=$res['id'];
?>
<a href="out.php?id=<?php echo $id;?>" onclick="myFunction()" target="_blank" onfocus="curTab">
<img src="admin/uploads/<?php echo $res['image']; ?>" id="click" style="width:200px;height:75px;" class="myImgClass" />
</a>
</td>
</tr>
<script>
$(".myImgClass").click(function(){
$(this).parent().css("background-color","green");
});
</script>
【解决方案2】:
@Ron 的回答是对的。如果你想像你一样使用onclick 属性(Ron 的回答中没有使用它),你会这样做:
在html中:onclick=myFunction(this)
在js中:
function myFunction(elt) {
$(elt).parent().css("background-color","green");
}