我正在尝试使用 jquery 显示/隐藏表的下两行。下面的代码当前显示了带有类 - quest_image 的 tr,但没有类 quest_text。
<table id="my_table">
<tr>
<td><img alt="Show/Hide" src="img.gif" class="ShowHide"></td>
<td>text 1</td>
</tr>
<tr class="quest_image">
<td colspan="2">hidden text 1</td>
</tr>
<tr class="quest_text">
<td colspan="2">hidden text 3</td>
</tr>
<tr>
<td><img alt="Show/Hide" src="img.gif" class="ShowHide"></td>
<td>text 2</td>
</tr>
<tr class="quest_image">
<td colspan="2">hidden text 4</td>
</tr>
<tr class="quest_text">
<td colspan="2">hidden text 2</td>
</tr>
</table>
<script type="text/javascript" src="../jquery/jquery/jquery-1.7.2.js"></script>
<script type="text/javascript">
// bind click event ONCE to the table
// not to every .ShowHide
$('#my_table').click(function(event){
// find where the click originated from
var trigger = event.target;
// if it has a class of "ShowHide"
// THEN toggle the next row
if(trigger.className == 'ShowHide')
{
// toggle away
$(trigger).closest('tr').next('.quest_text').toggle();
$(trigger).closest('tr').next('.quest_image').toggle();
}
});
</script>
演示:http: //jsfiddle.net/7TzJL/
为什么一个切换有效,而另一个无效?