我想在點擊另一個DIV時顯示一個DIV。我是JQuery的新手,只想使用CSS,但由於我使用PHP來循環顯示所有創建的DIV,因此我需要使用JQuery分別顯示隱藏的DIV。當如何使用JQuery切換功能在點擊時顯示/隱藏DIV?
HTML點擊:上desk_box節目點擊station_info有點側面時這樣用戶可以untoggle它
<div id="map_size" align="center">
<div id='desk_box' style='position:absolute;left:20px;top:60px;'>id:84</div>
<div id='station_info' style='position:absolute;left:20px;top:60px;'>Hello the id is:203</br>Section:Section C</br></div>
<script type="text/javascript">
$(document).ready(
function(){
$("#desk_box").click(function() {
$("#station_info").toggle();
});
});
</script>
</div><!--map size-->
CSS:
/*body*/
body{
margin:0px auto;
width:80%;
height:80%;
}
/*map size*/
#map_size{
width:1190px;
height:1300px;
background:#0099FF;
border-style: solid;
border-color: black;
position: relative;
}
/* desk boxes*/
#desk_box{
width: 23px;
height: 10px;
border: 4px solid black;
padding:10px;
}
/*station info*/
#station_info {
display: none;
width:150px;
height:150px;
border:4px solid black;
background-color:white;
}
#desk_box:hover ~ .station_info {
display: block;
}
PHP:
<?php
include 'db_conn.php';
//query to get X,Y coordinates from DB for the DESKS
$coord_sql = "SELECT coordinate_id, x_coord, y_coord FROM coordinates";
$coord_result = mysqli_query($conn,$coord_sql);
//see if query is good
if($coord_result === false) {
die(mysqli_error());
}
/*************************************/
//query to show workstation/desks information from DB for the DESKS
$station_sql = "SELECT coordinate_id, x_coord, y_coord, section_name FROM coordinates";
$station_result = mysqli_query($conn,$station_sql);
//see if query is good
if($station_result === false) {
die(mysqli_error());
}
?>
<?php
//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){
//naming X,Y values
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
//draw a box with a DIV at its X,Y coord
echo "<div id='desk_box' style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>id:".$id."</div>";
} //end while coord_result loop
while($row = mysqli_fetch_assoc($station_result)){
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];
echo "<div id='station_info'style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>Hello the id is:".$id."</br>Section:".$sec_name."</br></div>";
}
?>
的jsfiddle:http://jsfiddle.net/qmeepb36/
你的代碼工作,你只需要在頁面左側的菜單中包括jQuery:http://jsfiddle.net/qmeepb36/2/ – 2014-10-07 19:36:31
是在小提琴它的作品,但對我來說它不?我在
上包含了,該文件位於我的PHP文件的同一目錄中。任何想法? – mario 2014-10-07 19:40:11你檢查了控制檯是否有錯誤? – 2014-10-07 20:16:36