添加class =“active”到使用PHP的活动页面 [英] Add class="active" to active page using PHP

查看:94
本文介绍了添加class =“active”到使用PHP的活动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要< li> 标签在活动的dirrectory下更改...
现在,每个指南显示我如何做到当你的页面等于它,但我想改变
< li> 取决于在

I want the class of the <li> tag to change under the active dirrectory... now, every guide shows me how to do it when your page equals it, but i want to change the <li> depending on what dirrectory im on

上的目录,例如:

如果在
http://example.com/RESOURCES/code/opensource 上说,

http://example.com/RESOURCES/images/clipart < br>我想要RESOURCES^^ < li> 为class =active',其余显示'class =noactive'


或如果在
http://example.com / tutorials / css / flawless-dropdown-menu
我希望教程< li> 为class =active其余的都是'class =noactive'

if say im on
http://example.com/RESOURCES/code/opensource,
or
http://example.com/RESOURCES/images/clipart
i want the "RESOURCES" ^^ <li> to be 'class="active"' while the rest display 'class="noactive"'

or if im on
http://example.com/tutorials/css/flawless-dropdown-menu
I want the "tutorials" <li> to be 'class="active"' while the rest are 'class="noactive"'

这是我的网址显示方式的示例...

This is my example of how my url's are displayed...

http://example.com/tutorials/css/flawless-dropdown-menu

^^该URL是教程的页面... 。在tutorials目录下,而不是CSS类目录下,而不是页面标题(所有这些目录不是真实的,并且是从.htaccess重写的)[无关]

^^That URL is the page of a tutorial....under the "tutorials" directory, than under the "CSS" category directory, than the page title (all of these directories are not real and are rewrites from .htaccess) [irrelevant]

<ul id="mainnav">
  <li class="noactive"><a href="/">Home</a></li>
  <li class="active"><a href="/tutorials/">Tutorials</a></li>
  <li class="noactive"><a href="/resources/">Resources</a></li>
  <li class="noactive"><a href="/library/">Library</a></li>
  <li class="noactive"><a href="/our-projects/">Our Projects</a></li>
  <li class="noactive"><a href="/community/">Community</a></li>
</ul>


推荐答案

<ul id="mainnav">
    <li class="<?php if ($first_part=="") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Home</a></li>
    <li class="<?php if ($first_part=="tutorials") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Tutorials</a></li>
    <li class="<?php if ($first_part=="resources") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Resources</a></li>
    <li class="<?php if ($first_part=="library") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Library</a></li>
    <li class="<?php if ($first_part=="our-projects") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Our Projects</a></li>
    <li class="<?php if ($first_part=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="#">Community</a></li>
</ul>



PHP



PHP

<?php 
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>

这篇关于添加class =“active”到使用PHP的活动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆