我希望<li>
标签的类在活动目录下更改...现在,每个指南都会告诉我当你的页面等于它时如何做到这一点,但我想改变<li>
依赖于什么dirrectory我
例如:
如果说我在上
http://example.com/RESOURCES/code/opensource,
或
http://example.com/RESOURCES/images/clipart
我想要的"资源" ^^ <li>
是"类='活动’",而其余显示'class ="noactive"'
或者如果我正在使用
http://example.com/tutorials/css/flawless-dropdown-menu
我希望"教程" <li>
为'class ="active"',其余的是'类= "noactive"'
这是我的网址显示方式的例子......
http://example.com/tutorials/css/flawless-dropdown-menu
Run Code Online (Sandbox Code Playgroud)
^^该URL是教程的页面....在"tutorials"目录下,而不是在"CSS"类别目录下,而不是页面标题(所有这些目录都不是真实的,并且是从.htaccess重写的)[无关]
<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>
Run Code Online (Sandbox Code Playgroud)
Ale*_*ski 19
弄清楚答案......我在思考它.
<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>
Run Code Online (Sandbox Code Playgroud)
<?php
$directoryURI = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURI, PHP_URL_PATH);
$components = explode('/', $path);
$first_part = $components[1];
?>
Run Code Online (Sandbox Code Playgroud)
小智 13
header.php文件
$activePage = basename($_SERVER['PHP_SELF'], ".php");
Run Code Online (Sandbox Code Playgroud)
nav.php
<ul>
<li class="<?= ($activePage == 'index') ? 'active':''; ?>"><a href="/index.php">Home</a></li>
<li class="<?= ($activePage == 'tutorials') ? 'active':''; ?>"><a href="/tutorials.php">Tutorials</a></li>
...
Run Code Online (Sandbox Code Playgroud)
通过PHP,您可以尝试-
<?php
// gets the current URI, remove the left / and then everything after the / on the right
$directory = explode('/',ltrim($_SERVER['REQUEST_URI'],'/'));
// loop through each directory, check against the known directories, and add class
$directories = array("index", "tutorials","resources","library","our-projects","community"); // set home as 'index', but can be changed based of the home uri
foreach ($directories as $folder){
$active[$folder] = ($directory[0] == $folder)? "active":"noactive";
}
?>
<ul>
<li class="<?php echo $active['index']?>"><a href="/">Home</a></li>
<li class="<?php echo $active['tutorials']?>"><a href="/tutorials/">Tutorials</a></li>
<li class="<?php echo $active['resources']?>"><a href="/resources/">Resources</a></li>
<li class="<?php echo $active['library']?>"><a href="/library/">Library</a></li>
<li class="<?php echo $active['our-projects']?>"><a href="/our-projects/">Our Projects</a></li>
<li class="<?php echo $active['community']?>"><a href="/community/">Community</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)