【发布时间】:2016-08-09 11:36:27
【问题描述】:
假设我在 PHP 循环中有一个这样的表格,其中包含 <tr> 和 <td>:
//Some code...
echo "<table id='rowClick'>";
while($row=mysql_fetch_assoc($result){
echo "<tr><td><input type='text' value='{$row['item']}'></td></tr>";
}
echo "</table>";
//Rest of code
我使用了 CSS :
table tr.active{
background:red;
}
对于 JQuery:
<script>
$(document).ready(function(){
$("#rowClick").children("tbody").children("tr").children("td").keydown(function(){
$(this.parentNode).toggleClass("active");
});
$("#rowClick").children("tbody").children("tr").children("td").keyup(function(){
$(this.parentNode).toggleClass("active");
});
});
</script>
我对 JQuery 不是很熟悉。我想要的是,当用户在任何行中选择 <td> input field (或被聚焦)时,<tr> 的颜色将会改变。根据上面的jquery,它可以工作,但不如预期,因为每次我选择输入字段时它都会变成红色,然后在选择下一个字段时它会返回到默认表格颜色等等。
【问题讨论】:
-
让我试试这个,我会告诉你结果!