php - 链接 CSS 根据 URL 变化并保持子 URL 的样式

标签 php css

我正在处理以下类型的断断续续的代码,同时试图解决这个问题。

<?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/

相关文章:

php - 如何从 CLI 调用写入 APCu?

php - 根据插入请求自动从 SQL 表中删除旧记录

css - jQuery:在单击事件中将 css 应用于图像

php - 将数据从sql加载到html中的列表

javascript - 基于移动设备更改链接

html - z-index 无法正常工作

php - Laravel 中的用户关注系统

php - 依赖注入(inject)和序列化类

php - 使用 Advanced Custom Fields 和 Contact Form 7 显示表单

html - 如果视口(viewport)是移动的,我如何显示叠加层?