我有一個證明很困難的問題。我有一個論壇,對一個問題的多個迴應,從數據庫中拉出來,並使用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/ –