Php 我有我的动态页面,如何在当前页面上添加活动类?

Php 我有我的动态页面,如何在当前页面上添加活动类?,php,dynamic,addclass,Php,Dynamic,Addclass,我有四个php页面,homepage.inc.php,about.inc.php,contact.inc.php,这是我的index.php <div id="menu"> <nav> <a id="home" target="_self" href="index.php">Home</a> <a id="about" target="_self" href="index.php?p=about">

我有四个php页面,homepage.inc.php,about.inc.php,contact.inc.php,这是我的index.php

<div id="menu">
    <nav>
        <a id="home" target="_self" href="index.php">Home</a>
        <a id="about" target="_self" href="index.php?p=about">About us</a>
        <a id="contact" target="_self" href="index.php?p=contact">Contact</a>
    </nav>
</div>

<div id="content">
    <?php
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];


        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>

</div>
<?php
$homeCls = '';
$aboutCls = '';
$contactCls= '';
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];

       if($p == '') {
     $homeCls = 'active';
       } else if($p == 'about'){
         $aboutCls = 'active';
       } else if($p == 'contact'){
         $contactCls= 'active';
       }
        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>

这是我的css活动{背景:灰色;}
我想在菜单上添加一个活动类。如何操作?

您可以执行以下操作:

<a id="contact" target="_self" href="index.php?p=contact" <?php if ($_GET['p'] == "contact") {
    echo  'class="active"';
    } ?> >Contact</a>
<?php
$homeCls = '';
$aboutCls = '';
$contactCls= '';
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];

       if($p == '') {
     $homeCls = 'active';
       } else if($p == 'about'){
         $aboutCls = 'active';
       } else if($p == 'contact'){
         $contactCls= 'active';
       }
        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>

或者使用速记PHP(使代码看起来更干净)

<?php
$homeCls = '';
$aboutCls = '';
$contactCls= '';
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];

       if($p == '') {
     $homeCls = 'active';
       } else if($p == 'about'){
         $aboutCls = 'active';
       } else if($p == 'contact'){
         $contactCls= 'active';
       }
        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>

或者您可以在JQuery中使用一些JavaScript:

<script>
$('#<?php echo $_GET['p'] ?>').addClass('active');
</script>
<?php
$homeCls = '';
$aboutCls = '';
$contactCls= '';
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];

       if($p == '') {
     $homeCls = 'active';
       } else if($p == 'about'){
         $aboutCls = 'active';
       } else if($p == 'contact'){
         $contactCls= 'active';
       }
        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>

$('#').addClass('active');

如下所示更改代码 “>家 “>关于我们 “>联系方式

<?php
$homeCls = '';
$aboutCls = '';
$contactCls= '';
    $pages_dir = 'pages';
    if (!empty($_GET['p'])) {
        $pages = scandir($pages_dir, 0);
        unset($pages[0], $pages[1]);
        $p=$_GET['p'];

       if($p == '') {
     $homeCls = 'active';
       } else if($p == 'about'){
         $aboutCls = 'active';
       } else if($p == 'contact'){
         $contactCls= 'active';
       }
        if(in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');


        }else {
         echo 'Sorry, page not found.';
        }
    }else{
        include($pages_dir.'/home.inc.php');
        }
    ?>