Apply a specific class/id to current page on menu (PHP)(将特定的类/ID 应用于菜单上的当前页面 (PHP))
问题描述
我有一个这样的菜单:
<a href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a><a href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a><a href="http://domain.com/folder/gallery"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a><a href="http://domain.com/folder/dontknow"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
我想要一些可以自动将 class="current" 添加到我当前所在页面的内容.链接(如您在上面的代码中所见)类似于 domain.com/folder/biography 或 domain.com/folder/contacts,所以没有 .php/.html 等
我尝试过:
<a <?php if (strpos($_SERVER['PHP_SELF'], 'biography')) echo 'class="current"';?>href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a><a <?php if (strpos($_SERVER['PHP_SELF'], 'contacts')) echo 'class="current"';?>href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>......
但它不起作用......带strops的解决方案似乎可行,可能我做错了......:P
你应该:
- 用
!== false
检查strpos()
的结果. - 使用
$_SERVER['REQUEST_URI']
而不是$_SERVER['PHP_SELF']
. - 将代码包装在函数中.
像这样:
<div id="blahblah" style="blahblah"><a <?php get_current('biography') ?>href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a><a <?php get_current('contacts') ?>href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>......
I have a menu like this:
<div id="blahblah" style="blahblah">
<a href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
<a href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
<a href="http://domain.com/folder/gallery"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
<a href="http://domain.com/folder/dontknow"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
</div>
I'd like to have something that automatically adds a class="current" to the page I'm currently in. Links (as you can see in the code above) are like domain.com/folder/biography or domain.com/folder/contacts, so without .php/.html, etc.
I tried with:
<div id="blahblah" style="blahblah">
<a <?php if (strpos($_SERVER['PHP_SELF'], 'biography')) echo 'class="current"';?> href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'contacts')) echo 'class="current"';?> href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
...
...
</div>
But it doesn't work... the solution with strops seems viable, probably I'm doing it wrong.. :P
You should:
- check the result of
strpos()
with!== false
. - Use
$_SERVER['REQUEST_URI']
rather than$_SERVER['PHP_SELF']
. - Wrap the code inside a function.
Something like this:
<?php
function get_current($name) {
if (strpos($_SERVER['REQUEST_URI'], $name) !== false)
echo 'class="current"';
}
?>
<div id="blahblah" style="blahblah">
<a <?php get_current('biography') ?> href="http://domain.com/folder/biography"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
<a <?php get_current('contacts') ?> href="http://domain.com/folder/contacts"><img style="blahblah" src="YmxhaGJsYWhibGFoYmxhaA=="></a>
...
...
</div>
这篇关于将特定的类/ID 应用于菜单上的当前页面 (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将特定的类/ID 应用于菜单上的当前页面 (PHP)
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01