我正在尝试使用“.venue_table”类来定位表格内的元素。我想在每次单击元素时切换/取消切换突出显示的背景颜色。这不起作用:
<script>
$(document).ready(function(){
$(".income_table tr").click(function () {
$(this).toggleClass("toggled_tr");
});
});
</script>
我的代码有问题吗?
最佳答案
对我有用:http://jsfiddle.net/mplungjan/xBzPW/
<style>
.income_table { background-color:red }
.toggled_tr { background-color:yellow }
</style>
<script>
$(document).ready(function() {
$(".income_table tr").click(function () {
$(this).toggleClass("toggled_tr");
});
});
</script>
<table class="income_table">
<tr>
<td>Row 1 cell 1</td>
<td>Row 1 cell 2</td>
</tr>
<tr>
<td>Row 2 cell 1</td>
<td>Row 2 cell 2</td>
</tr>
</table>
关于Javascript 定位 tr 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866753/