我正在处理以下类型的断断续续的代码,同时试图解决这个问题。
<?php $curURL = ROOT.$_SERVER['REQUEST_URI']; ?>
<li><a href="<?php $href = APP_ROOT; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">HOME</a></li>
<li><a href="<?php $href = APP_ROOT.'about'; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">ABOUT</a></li>
<li><a href="<?php $href = APP_ROOT.'portfolio'; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">PORTFOLIO</a></li>
<li><a href="<?php $href = APP_ROOT.'services'; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">SERVICES</a></li>
<li><a href="<?php $href = APP_ROOT.'blog'; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">BLOG</a></li>
<li><a href="<?php $href = APP_ROOT.'contact'; echo $href; ?>" class="<?php if($href == $curURL) { ?>main_active<?php } ?>">CONTACT</a></li>
我目前得到的结果是,如果我在选定的页面 url (ex. http://localhost/myapp/index/)
上,那么它将相应地更改 css 样式。我希望发生的事情是,即使我在该页面的子 URL 上,CSS 样式仍将保持更改 (例如 http://localhost/myapp/index/dosomething/)
。一段时间以来,我一直在用头撞墙。
最佳答案
我在我的一个网站上就是这样做的。这是一个将类应用于事件链接的示例,但思路是一样的...
PHP
$current_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$fullPath = parse_url($current_url, PHP_URL_PATH);
$prepare = explode('/',$fullPath);
$path = "/" . $prepare[1];
HTML
<li><a href="location.php"<?php if($path == $thePath) { echo 'class="active"';}?>>Link Title</a>
关于php - 链接 CSS 根据 URL 变化并保持子 URL 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731487/