我有一个证明很困难的问题。我有一个论坛,对一个问题的多个回应,从数据库中拉出来,并使用foreach循环(PHP)显示。我想要一个'编辑你的响应'函数,并有一个jquery函数,它将'显示'一个包含每个响应下面输入的HTML块。问题是,当我在一个响应上选择编辑按钮时,它在每个响应下激活HRML,因为它的目标是类“theDiv”。有没有可能有一个jQuery的功能,可以选择只有一个.......即使foreach循环中的每个响应都有一个动态创建的唯一类,jQuery的功能可以针对?真正地奋斗,看看如何可以做到这一点.....在jQuery函数可以访问的PHP foreach循环中创建动态类
<html>
<head>
<style type="text/css">
.theDiv{
display:none;
}
</style>
<link href="styles/threads_page.css" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function() {
$(".show").click(function() {
$(".theDiv").show("normal");
});
$(".hide").click(function() {
$(".theDiv").hide("normal");
});
});
</script>
</head>
<body>
<?php foreach($responses as $response):?>
<h3><?php echo $response->author . "<br/>";
echo $response->content;
echo "<div class=\"theDiv\">
<form action=\"question_gallery.php\" method=\"post\" class=\"form\">
<table>
<tr>
<td>
Edit your response:
<input class=\"question_field\" name=\"question\"/>
</td>
</tr>
</table>
</form>
</div>
<table>
<tr>
<td>
<button class=\"show\">Edit response</button>
</td>
<td>
<button class=\"hide\">Close</button>
</td>
</tr>
</table>
";} ?>
<?php endforeach; ?>
</body>
</html>
使用'$(this)'来定位被点击的元素。 – Barmar
从'this'开始,遍历DOM到你想要的元素。 http://api.jquery.com/category/traversing/ –