当导航栏(不是用ul创建的)位于单独的包含文件中时,如何使用php在页面上显示悬停/选定类?

当导航栏(不是用ul创建的)位于单独的包含文件中时,如何使用php在页面上显示悬停/选定类?,php,navigation,include,Php,Navigation,Include,我设置了一个php模板,其中顶部和底部的公共信息位于单独的包含文件中。如何使悬停的类在当前页面时显示在每个页面上?(每个导航栏元素的效果都不同——它是单词左侧的一个小图标。) 谢谢 见下面的代码: <nav> <div class="page"> <a href="themes.php" class="themes">Themes</a> <a href="custom-gifts

我设置了一个php模板,其中顶部和底部的公共信息位于单独的包含文件中。如何使悬停的类在当前页面时显示在每个页面上?(每个导航栏元素的效果都不同——它是单词左侧的一个小图标。) 谢谢

见下面的代码:

<nav>
         <div class="page">
          <a href="themes.php" class="themes">Themes</a>
            <a href="custom-gifts.php" class="custom-gifts">Custom Gifts</a>
            <a href="about-us.php" class="about-us">About Us</a>
            <a href="testimonials.php" class="testimonials">Testimonials</a>
            <a href="contact-us.php" class="contact-us">Contact Us</a>
         </div>
         </nav>

(导航栏之所以没有列表和单独的sprint图像,是因为我将网站设计为响应性强,而在移动版中,导航条一个接一个地出现,而不是挨着一个。)

我希望我说的对。试试这个:

    <? $f=basename($_SERVER['PHP_SELF']); ?>

    <div class="page">
        <a href="themes.php" class="themes<?=($f=='themes.php'?' on':'')?>">Themes</a>
        <a href="custom-gifts.php" class="custom-gifts<?=($f=='themes.php'?' on':'')?>">Custom Gifts</a>
        <a href="about-us.php" class="about-us<?=($f=='about-us.php'?' on':'')?>">About Us</a>
        <a href="testimonials.php" class="testimonials<?=($f=='testimonials.php'?' on':'')?>">Testimonials</a>
        <a href="contact-us.php" class="contact-us<?=($f=='contact-us.php'?' on':'')?>">Contact Us</a>
    </div>

第一行是将当前文件的名称加载到变量
$f
中。在下面的几行中,我们对照文件名检查
$f
,并在
-标记的
-属性中检查它们是否相等,例如
class=“联系我们”


现在为
上的
添加一个CSS,您就拥有了它

PHP与此完全无关。PHP在服务器上运行,不可能看到鼠标在客户端上做什么。您最多可以使用PHP在“current”元素上设置一个类,css可以查找该元素,例如
.current:hover{…}
。感谢Marc的建议。你能解释一下我是怎么做的吗?太棒了,这正是我需要的。非常感谢!关于这个概念的另一个问题是——如何使用这个方法,使某个目录中的所有页面都有on类?例如,假设我希望/theme目录中的所有页面在类中都有主题?我可以指定php来查找目录名而不是单个页面名吗?@user2069532:请更详细地解释:是否希望所有
都使用该类?
    <? $f=basename($_SERVER['PHP_SELF']); ?>

    <div class="page">
        <a href="themes.php" class="themes<?=($f=='themes.php'?' on':'')?>">Themes</a>
        <a href="custom-gifts.php" class="custom-gifts<?=($f=='themes.php'?' on':'')?>">Custom Gifts</a>
        <a href="about-us.php" class="about-us<?=($f=='about-us.php'?' on':'')?>">About Us</a>
        <a href="testimonials.php" class="testimonials<?=($f=='testimonials.php'?' on':'')?>">Testimonials</a>
        <a href="contact-us.php" class="contact-us<?=($f=='contact-us.php'?' on':'')?>">Contact Us</a>
    </div>