【问题标题】:Create a php menu that highlights current tab创建一个突出显示当前选项卡的 php 菜单
【发布时间】:2011-01-14 01:25:18
【问题描述】:

所以我在一个看起来像这样的 php 文件中有一个菜单(这是整个文件。我对 PHP 完全陌生。)

menu.php:

<li id="current"><a href="#"><span>Home</span></a></li> 
<li><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li><a href="http://www.me.net/R"><span>Results</span></a></li> 
<li><a href="http://www.me.net/P"><span>Pictures</span></a></li> 
<li><a href="http://www.me.net/O.html"><span>Our Location</span></a></li>

现在在我的页面中我这样做(index.php):

<div id="tabs1" >
    <ul>
        <!-- CSS Tabs -->
        <?php include("menu.php"); ?>
    </ul>
</div>

所以我想做的就是将上面的行改为:

<?php include("menu.php?current=pictures"); ?>

这将使活动选项卡成为图片选项卡。我该怎么做?

【问题讨论】:

  • 最好离开include("menu.php");,然后在index.php中通过URL传递参数?current=pictures,然后使用变量?

标签: php html css menu


【解决方案1】:

试试这个:

<li <?php if($_GET['current'] == 'home') {echo 'id="current"'}?>><a href="#"><span>Home</span></a></li> 
<li <?php if($_GET['current'] == 'blog') {echo 'id="current"'}?>><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li <?php if($_GET['current'] == 'results') {echo 'id="current"'}?>><a href="http://www.me.net/R"><span>Results</span></a></li></li>
and so on....

【讨论】:

  • 由于某种原因无法正常工作。我完全按照你说的做了。
  • @Bob: 您当前的值是否来自查询字符串,例如,当您访问博客页面时,请像这样访问 blog.php?current=blog
【解决方案2】:

我认为它没有必要在服务器端完成(占用 CPU 周期)。

使用 javascript/CSS 来实现这一点。

【讨论】:

  • 使用服务器端解决方案的一个原因是某些浏览器,例如在慢速手机上运行的浏览器,可能会遇到 javascript。
【解决方案3】:

你也可以试试这个:

您的php 脚本

<?php
    $selected = "pictures";
    $current_id = ' id="current"';
    include "menu.php";
?>

这是你的菜单:

<ul>
<li <?php if ($selected == "pictures") print $current_id; ?>><a href="#"><span>Home</span></a></li> 
<li <?php if ($selected == "blog") print $current_id; ?>><a href="http://blog.me.net/"><span>Blog</span></a></li> 
<li <?php if ($selected == "home") print $current_id; ?>><a href="http://www.me.net/R"><span>Results</span></a></li> 
<li <?php if ($selected == "me") print $current_id; ?>><a href="http://www.me.net/P"><span>Pictures</span></a></li> 
<li <?php if ($selected == "contacts") print $current_id; ?>><a href="http://www.me.net/O.html"><span>Our Location</span></a></li>
</ul>

【讨论】:

    【解决方案4】:

    值得一看

    intelligent navigation

    【讨论】:

      【解决方案5】:
      <nav>
         <style>
            #active{
              color:#FFC801;
            }
         </style>
         <?php
            $activePage = basename($_SERVER['PHP_SELF'], ".php");
            ?>
         <ul>
            <li><a href="about.php" id="<?= ($activePage == 'about') ? 'active':''; ?>">About Us</a></li>
            <li><a href="mentors.php" id="<?= ($activePage == 'mentors') ? 'active':''; ?>">Mentors</a></li>
            <li><a href="tours.php" id="<?= ($activePage == 'tours') ? 'active':''; ?>">Tours</a></li>
            <li><a href="animation.php" id="<?= ($activePage == 'animation') ? 'active':''; ?>">Animation</a></li>
            <li><a href="blogs.php" id="<?= ($activePage == 'blogs') ? 'active':''; ?>">Blog</a></li>
            <li><a href="testimonials.php" id="<?= ($activePage == 'testimonials') ? 'active':''; ?>">Testimonials</a></li>
            <li><a href="press_media.php" id="<?= ($activePage == 'press_media') ? 'active':''; ?>">Press/Media</a></li>
            <li><a href="facts.php" id="<?= ($activePage == 'facts') ? 'active':''; ?>">Facts</a></li>
         </ul>
      </nav>
      

      【讨论】:

      • 请解释您是如何解决所提问题的。
      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2013-11-18
      • 2012-09-17
      • 1970-01-01
      相关资源
      最近更新 更多