以下是我的 HTML 代码:
<table>
{foreach in smarty starts}
<tr>
<td valign="top" id ="show_answer"><p id="show_ans"><b>View Answer : </b></p></td>
<td align="right" width="7%" valign="top">
</td>
</tr>
<tr>
<td valign="top" id ="hide_answer"><b>Answer is here</b><br/><p id="hide_ans" >Hide Answer</p></td>
<td align="right" width="7%" valign="top">
</td>
</tr>
{foreach in smarty ends}
</table>
我的 jQuery 代码在这里:
<script>
$(document).ready(function(){
$("#show_ans").click(function(){
$("#show_answer").hide(500);
$("#hide_answer").show(500);
});
$("#hide_ans").click(function(){
$("#hide_answer").hide(500);
$("#show_answer").show(500);
});
});
</script>
实际上有多个这样的<tr>
标签和多个相关的ID(即多个show_ans
和show_ans
ID)。现在,当我尝试使用我的代码时,它仅适用于第一条记录,因为它仅与第一个 id 组合匹配。
但是我必须使代码对每个 ids 组合单独可用。但我不知道如何分配动态生成的不同 id 并在我的 jQuery 代码中使用它们。简而言之,该功能应该适用于 individual <tr>
,因此 id 应该是唯一的,并且 jQuery 函数应该可以正常工作。
谁能帮我解决这个问题?提前致谢。