jquery函数传递变量

时间:2011-07-30 05:25:33

标签: php jquery

嗨我正在构建一个php mysql数据库分页页面,所以我在记录底部有2行长的记录列表我想要一个div,当它点击它上面的跨度时打开,我该如何设置jquery使它成为<p>的id并在jquery中扩展它

<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

2 个答案:

答案 0 :(得分: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";
    }
}              

像这样......

答案 1 :(得分: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命令,只需使用此代码段,它将自行解决哪个面板要滑动,根据单击的跨度。