开发者

Set active class to php include menu

开发者 https://www.devze.com 2023-03-18 21:27 出处:网络
I have a menu using the following code, <ol id=\"feature_list\"> <li><a href=\"index.php?action=profile\" class开发者_如何学JAVA=\"feature_current\">Profile</a></li>

I have a menu using the following code,

<ol id="feature_list">
    <li><a href="index.php?action=profile" class开发者_如何学JAVA="feature_current">Profile</a></li>
    <li><a ....></li>
</ol>

When clicked the same page is loaded and I use the following code to include the required php file.

if (($_GET['action']) == 'profile') {
    include('profile.php');     }
}

Now I want to set active class 'feature_current' to menu dynamically based on the link clicked , how can i do it ?


the same way as you include the file:

<li><a href="...?action=profile" 
<?php if(isset($_GET['action']) && $_GET['action']=='profile'){ 
echo 'class="feature_current"'; } ?>
>...</a>


<li>
  <a href="index.php?action=profile" class="feature_current
    <?php if ($_GET['action'] == 'profile') { echo ' active"'; } ?>
  ">
    Profile
  </a>
</li>

Elongate to show the code better, but use the $_GET in-line with the PHP to output a class, when necessary.

Your other option is to use javascript and look at the location variable and apply the class based on the link, but server-side solution is probably best.


$actionClass = isset($_GET['action']) ? $_GET['action'] : '';

switch($actionClass){

default:
$actionClass = 'featured_content';
break;

case 'profile':
$actionClass = 'profile-class-name';
break;
  etc....
}

and the html

   <ol id="feature_list">
        <li><a href="index.php?action=profile" class="<?php echo $actionClass;?>">Profile</a></li>
        <li><a ....></li>
    </ol>


This is a VERY SIMPLE solution: you can use PHP to grab from the URL the value you wish and inserting the grabbed value at a jQuery code like:

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

Where "#" is the value grabbed from the URL. For example:

<?php

echo '<script type="text/javascript">
$(document).ready(function(){';
if(intval($_GET['PageID']) > 0) {
    $from_url = intval($_GET['PageID']);
    } else {
        if(isset($_GET['action'])) {
        $from_url = $_GET['action'];
        } else {
        $from_url = 'index';
        }
    }
echo '
    $(\'' . $from_url . '\').addClass(\'active\');
    });
</script>';

?>

Also explained here.

0

精彩评论

暂无评论...
验证码 换一张
取 消