-1

我有 3 页。根据我所在的页面,我想更改活动类别以反映这一点。

这 3 个页面是索引、博客和联系方式。它们每个都有子页面,比如 Work 有用于各种项目的各种 PHP 文件。博客有各种博客文章。Contact 只是一个 PHP 联系表格。

我想根据这个人的状态,移动 class="active" 。现在它在每一页上都是硬编码的。

我的代码是:

<ul class="list-inline" id="menu">
    <li class="active">
        <a href="index.php">work</a>
    </li><!--
    --><li>
        <a href="blog.php">blog</a>
    </li><!--
    --><li>
        <a href="contact.php">contact</a>
    </li><!--
    --><li>
        <a href="#"><img class="social" src="img/icon/icon-facebook.png" alt="facebook"></a>
        <a href="#"><img class="social" src="img/icon/icon-twitter.png" alt="twitter"></a>
    </li>
</ul>
4

4 回答 4

0
<ul class="list-inline" id="menu">
<li<?php if ($_SERVER['SCRIPT_FILENAME'] == "index.php") { echo " class=\"active\""; } ?>>
    <a href="index.php">work</a>
</li>
<li<?php if ($_SERVER['SCRIPT_FILENAME'] == "blog.php") { echo " class=\"active\""; } ?>>
    <a href="blog.php">blog</a>
</li>
<li>
    <a href="contact.php">contact</a>
</li>
<li>
    <a href="#"><img class="social" src="img/icon/icon-facebook.png" alt="facebook"></a>
    <a href="#"><img class="social" src="img/icon/icon-twitter.png" alt="twitter"></a>
</li>
</ul>

等等

于 2013-10-15T18:42:21.327 回答
0

有很多方法,其中一种是:

<?php
$page = ''; // @TODO: assign with the right value
$work_class = $page == 'work' ? 'active' : '';
$blog_class = $page == 'blog' ? 'active' : '';
$contact_class = $page == 'contact' ? 'active' : '';
?>

<ul class="list-inline" id="menu">
    <li class="<?=$work_class?>">
        <a href="index.php">work</a>
    </li><!--
    --><li class="<?=$blog_class?>">
        <a href="blog.php">blog</a>
    </li><!--
    --><li class="<?=$contact_class?>">
        <a href="contact.php">contact</a>
    </li><!--
    --><li>
        <a href="#"><img class="social" src="img/icon/icon-facebook.png" alt="facebook"></a>
        <a href="#"><img class="social" src="img/icon/icon-twitter.png" alt="twitter"></a>
    </li>
</ul>

要获得更准确的答案,如果您提供有关其余代码外观的更准确信息,这将很有用。

于 2013-10-15T18:42:47.253 回答
0

我会循环它..

<?php
$base = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'], '/')-1); // Something like this to determine the current file such as 'blog.php'
$links = array();
$links['work'] = 'index.php';
$links['blog'] = 'blog.php';
// ..

foreach($links AS $name => $url)
{ ?>
    <a href="<?php echo $url; ?>"
    <?php if($base == $url) ?> class="active"<?php } ?>
    ><?php echo $name; ?></a>
<?php } ?>

您也可以添加活动类来包装 li 元素。

于 2013-10-15T18:58:23.437 回答
0

添加这个 PHP:

<?php #add class .active to current page
   $directoryURL = $_SERVER['REQUEST_URI'];
   $path = parse_url($directoryURL, PHP_URL_PATH);
   $components = explode('/', $path);
   $currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));

   if ($currentPage == "") {
       $currentPage = "index";
   }

   function href($url) {
      global $currentPage;
      $path = explode('/', $url);
      $page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
      echo 'href="' . $url . '"';

      if ($page == $currentPage) {
         echo 'active';
      }
   }
?>

将您的菜单调整为:

<li><a <?php href('about.php');?>>About</a></li>

*这应该工作。祝你好运

于 2014-03-06T12:39:50.683 回答