gpt4 book ai didi

php - 将特定类/id 应用到菜单上的当前页面 (PHP)

转载 作者:行者123 更新时间:2023-12-02 22:40:16 25 4
gpt4 key购买 nike

我有一个这样的菜单:

<div id="blahblah" style="blahblah">
<a href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a>
<a href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a>
<a href="http://domain.com/folder/gallery"><img style="blahblah" src="blahblahblahblah"></a>
<a href="http://domain.com/folder/dontknow"><img style="blahblah" src="blahblahblahblah"></a>
</div>

我想要一些东西可以自动将 class="current"添加到我当前所在的页面。链接(如您在上面的代码中所见)类似于 domain.com/folder/biography 或 domain .com/folder/contacts,所以没有 .php/.html 等。

我试过:

<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="blahblahblahblah"></a>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'contacts')) echo 'class="current"';?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a>
...
...
</div>

但它不起作用......带 strops 的解决方案似乎可行,可能我做错了......:P

最佳答案

你应该:

  1. 使用 !== false 检查 strpos() 的结果。
  2. 使用 $_SERVER['REQUEST_URI'] 而不是 $_SERVER['PHP_SELF']
  3. 将代码包装在一个函数中。

像这样:

<?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="blahblahblahblah"></a>
<a <?php get_current('contacts') ?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a>
...
...
</div>

关于php - 将特定类/id 应用到菜单上的当前页面 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10884624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com