dqyitt2954 2017-01-14 17:41
浏览 53

在PHP中单击后更改并保留特定的a-tag背景颜色

I've got a a-tag dynamically populated which extracts data from a database and multiplies to the number of records existing. So this to be looked at more like an item in a list.

echo "<a class='Keylist ".$wecb_color."' id='myIDhere' href='index.php?idd=".$record['categoryid']."'><div>".$record['descriptor']."</div></a>";

I'm trying to keep one of the a-tags background set on a different color when a user clicks. Behavior I'm trying to achieve is when a user scrolls and clicked on one link, that a-tag to remain selected depicting a different background color.

I've written snippet trying to do that.

$wecb_color ='';
if(isset($_GET['idd'])){
    $_SESSION['link'] = $_GET['idd'];
    echo "<br><br><br><br><br>".$_SESSION['link'];

    if($_GET['idd'] == $_SESSION ['link']) {
    $wecb_color = 'changetogreen';
    }

}

The issue is this makes all the a-tag's which are generated having a background color changed to green instead the one that user clicks.

Hard coded CSS script reads as :- .changetogreen{background-color: green;}

Any thoughts appreciated.

  • 写回答

1条回答 默认 最新

  • doushi1960 2017-01-14 18:18
    关注

    It'd change the background color of all links because you're changing the whole class style. Try this:

    echo "<a class='"Keylist " style=" '.$wecb_style.' ;" id='myIDhere' href='index.php?idd=".$record['categoryid']."'><div>".$record['descriptor']."</div></a>";
    

    So you can change the style of particular tag. And PHP would be:

    $wecb_style ='';
    if(isset($_GET['idd'])){
        $_SESSION['link'] = $_GET['idd'];
        echo "<br><br><br><br><br>".$_SESSION['link'];
    
        if($_GET['idd'] == $_SESSION ['link']) {
        $wecb_style = 'background: green';
        }
    
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?