duaner1634 2014-05-13 16:32
浏览 32
已采纳

将jquery slidetoggle应用于来自php的一段时间的结果

The following piece of code in php, uses a while statement to output variable $the_job_id. For each of the output I want to apply jquery slidetoggle. The problem is that in my code slidetoggle works only for the first output of my while. Not working for the rest. Any idea how i should modify my code in order slidetoggle to work for each of my while outputs?

This is my php code:

<?php

$result = mysql_query("select * from `user_job` where `job_id` IN ($all_saved_job_id) ");

while($run_job = mysql_fetch_array($result)){

  $the_job_id = $run_job['job_id']; 

echo"<div id='flip'> PRESS TO SLIDE </div>";

echo"  <div id='panel'>  $the_job_id </div>";

}// end while

?>

this is my script :

<script> 

$(document).ready(function(){
  $("#flip").click(function(){
     $("#panel").slideToggle("slow");
  });
});

</script>

this is my css :

<style>

#flip{
cursor:pointer;
margin-left:100px;
}

#panel{
padding:0px;
display:none;
}       

</style>
  • 写回答

1条回答 默认 最新

  • doutang7414 2014-05-13 16:33
    关注

    id must be unique, otherwise you'll always get the first element in the page with duplicated id, so you need to use class instead:

    echo"<div class='flip'> PRESS TO SLIDE </div>";
    
    echo"  <div class='panel'>  $the_job_id </div>";
    

    then you can use . to target elements by class name:

    $(document).ready(function(){
        $(".flip").click(function(){
            $(this).next().slideToggle("slow");
        });
    });
    

    Please note that you also need to change your CSS selector using . instead of # accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度