duangang79177 2012-02-09 06:43
浏览 25
已采纳

使用PHP仅在网站根目录上添加活动类

I included my header in every page which also contains the navigation which leads to the problem on how to add an active class to each nav element if the specific link gets clicked.

I found a working solution on the net.

I put this in my index.php on top:

<?php basename($_SERVER["REQUEST_URI"]) ?>

For each link I do this:

<li><a <?php if (basename($_SERVER["REQUEST_URI"]) == "index.php") echo " class='active' ";?> href="index.php">Start</a></li>

Everything works fine but of course it doesn't work at the very start if you just enter the domain name like:

www.mydomain.com

I tried:

if (basename($_SERVER["REQUEST_URI"]) != " ")

But then it will stick with the active class all the time.

How can I add something like "if it's the root just give the "Start" link and only then the active class?

  • 写回答

3条回答 默认 最新

  • douxie0824 2012-02-09 06:57
    关注

    You could try to use try $_SERVER['PHP_SELF'] instead of $_SERVER["REQUEST_URI"]

    ps: if you want to know more about php configuration (including the values of $_SERVER and others) create a php file with <?php phpinfo(); ?> and call it from browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题