如何使用PHP动态插入CSS类? [英] How do I dynamically insert a CSS class using PHP?

查看:209
本文介绍了如何使用PHP动态插入CSS类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有什么问题?当点击相应的< li> 元素时,我想动态插入currentCSS类。谢谢!

What's wrong with the following code? I would like to dynamically insert "current" CSS class when the respective <li> element is clicked. Thanks!

<?php 
$pg = $_SERVER['PHP_SELF']; 
?>

<section>
    <aside>       
        <nav>
            <ul>
                <li class="<?php if($pg == 'index.php?page=about.php'){echo 'current';} ?>"><a href="index.php?page=about.php">About</a></li>                       
            <li class=""><a href="">Services</a></li> 
            </ul>
        </nav>
    </aside>
<section>


推荐答案

尝试:

<?php 
$pg = $_GET['page'];

$allow = array('about.php', 'main.php', 'other.php');
if ( !in_array($pg, $allow) ) {
    $pg = 'default_value';
}
?>

<section>
    <aside>       
        <nav>
            <ul>
                <li class="<?php if($pg == 'about.php'){echo 'current';} ?>"><a href="index.php?page=about.php">About</a></li>                       
            <li class=""><a href="">Services</a></li> 
            </ul>
        </nav>
    </aside>
<section>

这篇关于如何使用PHP动态插入CSS类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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