dongqian2021 2011-08-10 02:40
浏览 69
已采纳

PHP foreach,CSS和jQuery脚本..如何进行交互?

i'm trying to use a jQuery show/hide script on a PHP file, and it goes like this:

<?php
foreach ($empresas as $empresa) {
echo "<style type='text/css'>
.$empresa[teaserid] { 
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
.showhide$empresa[teaserid] {
    display:none;
}
</style>";
echo '<script type="text/javascript">';
echo ' $(document).ready(function(){ ';
echo '         $(\".$empresa[teaserid]\").hide(); ';
echo '         $(\".showhide$empresa[teaserid]\").show(); ';
echo '  $(\".showhide$empresa[teaserid]\").click(function(){ ';
echo '  $(\".$empresa[teaserid]\").slideToggle(), 500; ';
echo '  }); ';
echo ' });';
echo '</script>';
echo "<a href='#' class='showhide$empresa[teaserid]'>$empresa[titulo]</a><br />
    <div class='$empresa[teaserid]'>
TEST</div>";
}
?>

So, what I need is a foreach in php that echoes new CSS values and a new jQuery script. because each DIV needs different CSS and jQuery to relate and be able to show and hide its content. This echoing didn't work. The CSS goes ok, but the jQuery doesn't with the PHP $strings. What can I do? Or there's a simpler way to do this? A jQuery function that relates to any current div alone ? Thanks anyone who helps me in this one..

  • 写回答

3条回答

  • douyan8267 2011-08-10 02:55
    关注

    In PHP, you can only put variables in double quote strings:

    $a = 'Jon';
    echo "Hi $a"; // Should output 'Hi Jon'
    echo 'Hi $a'; // should output 'Hi $a'
    

    So if you want the Javascript to read:

    $(".Jon") // where Jon is the value of $a
    

    Then in PHP you can output it this way:

    echo '$(\".' . $a . '\")'; // Notice we are splitting the echo statement into a concatenation of 3 strings
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示