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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘