2014-10-07 69 views
0

我想在点击另一个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/

+0

你的代码工作,你只需要在页面左侧的菜单中包括jQuery:http://jsfiddle.net/qmeepb36/2/ – 2014-10-07 19:36:31

+0

是在小提琴它的作品,但对我来说它不?我在上包含了,该文件位于我的PHP文件的同一目录中。任何想法? – mario 2014-10-07 19:40:11

+0

你检查了控制台是否有错误? – 2014-10-07 20:16:36

回答

0

你的代码似乎工作,在jfiddle只需选择“框架&扩展”,jQuery的而不是没有图书馆

+0

是在小提琴它的作品,但对我来说它不?我包括在和该文件是在我的PHP文件相同的目录..任何想法 – mario 2014-10-07 19:42:45

0

选择小提琴jQuery的框架,有一个工作的例子,然后添加一个保留到#station_info让你总能看到#desk_box div。

#station_info { 
    display: none; 
    width:150px; 
    height:150px; 
    margin-left:100px; // <- this 
    border:4px solid black; 
    background-color:white; 
} 

http://jsfiddle.net/qmeepb36/3/

+0

是在小提琴它的作品,但对我来说不?我包括了在和该文件是在我的PHP文件的同一目录..任何想法 – mario 2014-10-07 19:43:09

+0

呃,很奇怪。你可以与我们分享一个链接,网页放置在哪里?没有其他信息很难理解你有什么样的问题。 – Monte 2014-10-07 19:47:52

+0

实际上它的工作原理是只有一个,但我想要做的是使每个DIV与我的PHP while循环创建。 – mario 2014-10-07 20:19:36

0

你会发现你的Jquery只影响它找到的第一个#desk_box。

当你有多个盒子时,你需要稍微区分它们。

<a class="showSingle" target="1">Div 1</a> 

<a class="showSingle" target="2">Div 2</a> 

<a class="showSingle" target="3">Div 3</a> 

<a class="showSingle" target="4">Div 4</a> 

<div id="div1" class="targetDiv">Lorum Ipsum1</div> 
<div id="div2" class="targetDiv">Lorum Ipsum2</div> 
<div id="div3" class="targetDiv">Lorum Ipsum3</div> 
<div id="div4" class="targetDiv">Lorum Ipsum4</div> 

jQuery(function() { 
    jQuery('.showSingle').click(function() { 
     var index = $(this).index(), 
      newTarget = jQuery('.targetDiv').eq(index).slideDown(); 
     jQuery('.targetDiv').not(newTarget).slideUp(); 

    }); 
}); 

看这个的jsfiddle为例: http://jsfiddle.net/jakecigar/XwN2L/2154/

0

我只是编辑的代码为您更好地了解

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; 
} 

HTML:

<script type="text/javascript"> 
      $(document).ready(function() { 
       $(".desk_box").click(function() { 
       $id = $(this).attr("data") 
       $("#station_info_"+$id).toggle(); 
       }); 


      }); 
     </script> 

PHP:

<?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 class ='desk_box' data = ".$id." id='desk_box_".$id."'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_cor = $row['coordinate_id']; 
         $x_pos = $row['x_coord']; 
         $y_pos = $row['y_coord']; 
        $sec_name = $row['section_name']; 

        echo "<div id='station_info_".$id_cor."' style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>Hello the id is:".$id_cor."</br>Section:".$sec_name."</br></div>"; 
        } 
       ?> 

检查JSfiddle明白了什么,我试图做http://jsfiddle.net/hiteshbhilai2010/arfLboy7/10/

,同时可以创建div我试图做一下DIV desk_boxstation_div之间有一定的参考进行切换。