[英]How to create a toggle function in JavaScript to display a div in HTML?
I am trying to create a "drop down" div. 我正在尝试创建一个“下拉” div。 The idea is that when you press a button the display property of the div is changed to BLOCK.
这个想法是,当您按下按钮时,div的显示属性将更改为BLOCK。 I have managed to get the div to display but i cannot get the div to "hide" once the user clicks the button once again.
我设法使div显示出来,但是一旦用户再次单击按钮,我就无法使div“隐藏”。 On button click, i am calling a javascript fucntion that will get the element id and change the property to BLOCK.
单击按钮时,我正在调用javascript功能,它将获取元素ID并将属性更改为BLOCK。
this is my html which is created by a while loop. 这是我的html是由while循环创建的。
<?php
$sql="SELECT * FROM planservers WHERE plans_id = $planID";
$retServerNames = mysql_query($sql);
$buttonId=0;
While($row1=mysql_fetch_array($retServerNames)){
$serverID = $row1['id'];
$serverName = $row1['servername'];
/**************this is my button.*******************/
echo"<input type='button' id='$buttonId' class='btn' name='show' value='".$serverName."' onclick='javascript:showServer(".$buttonId.");'
style='width: 90%; margin: 1%; text-align:left;'>";
echo"<div id='$serverName' style='margin: 3%; display:none;'>";
echo"<table style='border:none;' cellspacing='10px'>
<thead>
<tr>
<th></th>
<th>Task</th>
<th>Status</th>
<th>Agent</th>
<th>Date Completed</th>
<th></th>
</tr>
</thead>";
$sql2="SELECT * FROM planservertasks WHERE plans_id =$planID and planservers_id=$serverID";
$reTasks = mysql_query($sql2);
$counter = 1;
while($row=mysql_fetch_array($reTasks)){
if($row['status'] == 0){
$statusString = "Pending";
$statColor = "<td id=".$row['id']." style='color:orange;font-weight: bold;'>";
}else{
$statusString ="Completed";
$statColor = "<td id=".$row['id']." style='color:green;font-weight: bold;'>";
}
echo"<tbody>
<tr>
<td>".$counter.".</td>
<td>".$row['taskname']."</td>
".$statColor.$statusString."</td>
<td >".$row['completedby']."</td>
<td>".$row['completdate']."</td>
<td><input type='button' name='$serverName' value='Completed' onclick='javascript:completeTask(".$row['id'].");'></td>
</tr>
</tbody>";
$counter++;
}
echo"</table>";
echo"</div>";
$buttonId++;
}
?>
This is my javascript function. 这是我的javascript函数。
function showServer(buttonid)
{
var servername = document.getElementById(buttonid).value;
if(document.getElementById(servername).style.display = "none"){
document.getElementById(servername).style.display = "block";
}
}
this is the collapsed view. 这是折叠视图。
this is the expanded view.
这是展开的视图。
I need to be able to click the blue button with the server name again to collapse the div. 我需要能够再次单击服务器名称的蓝色按钮以折叠div。 I am not sure if i need to change my java function or my html.
我不确定是否需要更改我的Java函数或HTML。 any suggestions will help.
任何建议都会有所帮助。
Pretty self-explanatory 相当不言自明
function showServer(buttonid)
{
var servername = document.getElementById(buttonid).value;
if(document.getElementById(servername).style.display == "none"){
document.getElementById(servername).style.display = "block";
}else {
document.getElementById(servername).style.display = "none"; }
}
if(document.getElementById(servername).style.display = "none"){
document.getElementById(servername).style.display = "block";
}else {
document.getElementById(servername).style.display = "none";
}
In this code, your if statement isn't comparing to 'none'. 在此代码中,您的if语句未与“ none”进行比较。 The way you have it written, would assign 'none'.
您编写的方式将分配为“无”。 It should be == 'none'.
它应该=='none'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.