当前页面高亮 [英] Current page highlight

查看:104
本文介绍了当前页面高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一切正常,但是当我第一次进入页面时,域中的/之后没有任何东西姓名。



例如:mydomain.com /



如何突出显示该页面。它只有当我有/index.php时才会突出显示



例如:mydomain.com/index.php



这是我的html:

 < nav> 
< ul class =cd-primary-nav>
< li>< a href =index.php<?= echoSelectedClassIfRequestMatches(index)?>>>主页< / a>< / li>
< li>< a href =about.php<?= echoSelectedClassIfRequestMatches(about)?>>关于< / a>< / li>
< li>< a href =services.php<?= echoSelectedClassIfRequestMatches(services)?>>> Services< / a>< / li>
< li>< a href =brands.php<?= echoSelectedClassIfRequestMatches(brands)?>>>品牌< / a>< / li>
< li>< a href =testimonials.php<?= echoSelectedClassIfRequestMatches(testimonials)?>>评论< / a>< / li>
< li>< a href =contact.php<?= echoSelectedClassIfRequestMatches(contact,thankyou)?>>联络人< / a>< / li>
< / ul>
< / nav>

这里是PHP:

 <?php 
function echoSelectedClassIfRequestMatches($ requestUri)
{
$ current_file_name = basename($ _ SERVER ['REQUEST_URI'],.php);

if($ current_file_name == $ requestUri)
echo'class =selected';
}
?>

预先致谢。

解决方案

您实际上可以使用 $ _ SERVER ['SCRIPT_NAME'] 来获得执行文件名。即使您浏览 example.com ,它仍将包含 index.php 作为值。


I'm using php to highlight my current page.

Everything works correctly however when I first go to the page there is nothing after "/" in the domain name.

Ex: mydomain.com/

How can I highlight that page. It only gets highlighted when I have /index.php

Ex: mydomain.com/index.php

Here's my html:

<nav>
    <ul class="cd-primary-nav">
      <li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
      <li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a></li>
      <li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a></li>
      <li><a href="brands.php" <?=echoSelectedClassIfRequestMatches("brands")?>>Brands</a></li>
      <li><a href="testimonials.php" <?=echoSelectedClassIfRequestMatches("testimonials")?>>Reviews</a></li>
      <li><a href="contact.php" <?=echoSelectedClassIfRequestMatches("contact" , "thankyou")?>>Contact</a></li>
    </ul>
  </nav>

Here's the PHP:

<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
    $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");

    if ($current_file_name == $requestUri)
        echo 'class="selected"';
}
?>

Thanks in advance.

解决方案

You can actually use $_SERVER['SCRIPT_NAME'] to get the executed filename. Even if you browse to example.com, it will still contain index.php as the value.

这篇关于当前页面高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆