【发布时间】:2016-09-24 13:13:43
【问题描述】:
我创建这段代码来显示和隐藏 div 这段代码可以工作,但是当我将它与 php 一起使用时,当我单击第一个 div 的显示时,他向我展示了 div 但是当我点击第二个时,他告诉我第一个而不是第二个如何解决这个问题?
include("connect.php");
$sql=mysqli_query($conn,"select * from tbl_codelibrary order by db_id desc")or die(mysqli_error($conn));
echo'<div id="no-more-tables">
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">';
echo"<tr>";
echo"<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center' rowspan='2'>Title</th>
<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center' rowspan='2'>Code</th>";
echo"</tr></thead><tbody>";
while($row=mysqli_fetch_array($sql)){
$title=$row['db_title'];
$code=$row['db_code'];
echo"<tr>";
echo"<td data-title='Title'>";echo $title;echo'<div id="opener"><a href="#1" name="1">Show Code</a></div>';echo"<br/>";echo'<div id="upbutton"><a onclick="return hide();">Hide Code</a></div>'; echo"</td>";
echo"<td data-title='Code'>";echo"<pre><code class='' ><";?><?php echo $row['db_code'];?><?php echo"></code></pre>";echo"</td>";
}
?>
</body>
<script>
function show() {
if(document.getElementById('benefits').style.display=='none') {
document.getElementById('benefits').style.display='block';
}
return false;
}
function hide() {
if(document.getElementById('benefits').style.display=='block') {
document.getElementById('benefits').style.display='none';
}
return false;
}
</script>
【问题讨论】:
-
你可以使用.toggle()来显示和隐藏
-
@guradio 那是一个 jQuery 函数..
-
@Xorifelse 它被标记为 jQuery
-
@VishnuBhadoriya 没有为一个 div 复制我的代码工作,但是当我在 php 中有一个循环时,他为第一个工作,另一个显示第一个 div,而不是与点击相关的 div
标签: javascript php mysql