2011-07-30 53 views
0

嗨,我建立一个php mysql数据库分页页面,所以我有记录的2行长在记录底部的列表我想要一个div,当点击上面的跨度时打开,如何我设置了jQuery使它如此,它需要的<p>的ID,并扩大它的jQueryjquery函数传递变量

<span id="button1">Toggle</span> 

<p id="p1"> 
hello<br/>hello 
</p> 

<script> 
$("#button1").click(function() { 
$("#p1").slideToggle("slow"); 
}); 
</script> 

当我将它输出在PHP MySQL的按钮都将有不同的ID和p会不同的ID以及

+0

由于您将“button1”输出到您的PHP代码中的“id”字段中,因此您还可以在您的PHP代码中将“button1”输出到jQuery代码中。换句话说,名称是动态的并不重要,因为您可以在两个地方设置它们。 –

+0

你怎么样给你一个班级名字?这样你可以一次选择全部 – Ibu

回答

0
//Jquery Code which calls toggle_visibility function 
    $(".pOne").click(function(){ 
       toggle_visibility('partsIdThree'); 
      }) 
      .toggle(function() { 
       $(this).children("span").text("[-]"); 
      }, function() { 
       $(this).children("span").text("[+]"); 
      }); 

    //HTML Code 
<div id="parts_toogle_one">  
<p class="pOne">Add Records <span>[+]</span></p> 
     <div id="msg_body_one"> 
     <tr><td></td></tr> 
     </div> 
</div>  

    // Javascript code 
function toggle_visibility(id) { 
    var e = document.getElementById(id); 
    if(e.style.display == "inline") { 
     e.style.display = 'none'; 
    } 
    else if(e.style.display == "none") { 
     e.style.display = "inline"; 
    } 
}    

类似这样的..

0

您可以使用动态ID检索,所以你不必担心结果的数量..

例如:

<?php $i = 0;?> 
<?php foreach($resultset as $result) : ;?> 

<span class="activator" id="result<?php echo $i;?>">CLICK HERE</span> 
<div id="panel_result<?php echo $i;?>">SLIDER DIV</div> 

<!-- Do the rest of you processing of $result here; it just doesn't matter, for here you only need some identification, I used a number for convenience. --> 

<?php ++$i;?> 
<?php endforeach;?> 

你的jQuery:

$('.activator').click(function(){ 
var the_id = $(this).attr('id'); 
var the_panel = $('#panel_' + the_id); 
$(the_panel).slideToggle('slow'); 
}); 

因此,您不必为在php中打印的每一行编写jQuery命令,只需使用此代码片段即可自行选择要滑动哪个面板,根据哪个范围单击。

+0

哇,非常感谢你达米恩,这真的很有意义 – Andie

+0

这是写得很漂亮 – Andie