2014-02-14 41 views
1

我爲我的側邊菜單html使用頭文件。當然,我有多個頁面,有時會選中它們,當它們變爲選定類別時,就會變爲背景顏色。我正在實施purecss.io中的側邊菜單和博客組合。有沒有辦法,仍然使用一個頭文件,讓選定的菜單項更改類/和顏色? This is what the code looks like and the menu on the left, I want the different items to change in correlation with whatever page I'm on while still using the one header file.在使用頭文件時更改選定的鏈接

我基本上要更改類「純菜單中選擇」根據我是在

<ul> 
    <li><a href="newest.php">Newest</a></li> 
    <li><a href="trending.php">Trending</a></li> 
    <li><a href="following.php" >Following</a></li> 
    <li><a href="followers.php">Followers</a></li> 
    <li><a href="javascript:create()">Create</a></li> 
    <li><a href="search.php">Explore</a></li> 
    <li class="pure-menu-selected"><a href="home.php">Feed</a></li> 
    <li><a href="search-user.php">Search</a></li> 
    <li class="menu-item-divided"><a href="profile.php"><?php echo "$firstname";?></a></li> 
    <li><a href="settings.php">Settings</a></li> 
    <li><a href="account.php">Account</a></li> 
    <li><a href="logout.php">Log Out</a></li> 
</ul> 
+0

參見[這裏](http://stackoverflow.com/questions/16745263/php-how-to-securly-set- active-link-on-page-using-includes?lq = 1)或[here](http://stackoverflow.com/questions/8476225/php-set-active-link-on-page-using-includes)for不同的方案。 – olger

回答

1

頁面上的主要思想是檢查你的請求URL,如果它是你作爲選擇

<?php 
$url = $_SERVER['REQUEST_URI']; 
$selected = 'class="pure-menu-selected"'; 
>? 
<ul> 
    <li <?php echo (strpos($url, 'newest.php') !== false) ? $selected : '' ?>><a href="newest.php">Newest</a></li> 
    <li <?php echo (strpos($url, 'trending.php') !== false) ? $selected : '' ?>><a href="trending.php">Trending</a></li> 
    <li <?php echo (strpos($url, 'following.php') !== false) ? $selected : '' ?>><a href="following.php" >Following</a></li> 
    <li <?php echo (strpos($url, 'followers.php') !== false) ? $selected : '' ?>><a href="followers.php">Followers</a></li> 
    <li><a href="javascript:create()">Create</a></li> 
    <li <?php echo (strpos($url, 'search.php') !== false) ? $selected : '' ?>><a href="search.php">Explore</a></li> 
    <li <?php echo (strpos($url, 'home.php') !== false) ? $selected : '' ?>><a href="home.php">Feed</a></li> 
    <li <?php echo (strpos($url, 'search-user.php') !== false) ? $selected : '' ?>><a href="search-user.php">Search</a></li> 
    <li <?php echo (strpos($url, 'profile.php') !== false) ? $selected : '' ?>><a href="profile.php"><?php echo "$firstname";?></a></li> 
    <li <?php echo (strpos($url, 'settings.php') !== false) ? $selected : '' ?>><a href="settings.php">Settings</a></li> 
    <li <?php echo (strpos($url, 'account.php') !== false) ? $selected : '' ?>><a href="account.php">Account</a></li> 
    <li <?php echo (strpos($url, 'logout.php') !== false) ? $selected : '' ?>><a href="logout.php">Log Out</a></li> 
</ul> 
+0

非常感謝這實際上確實有助於解決問題。非常感謝。 – noahdotgansallo

+0

我很高興爲您提供幫助。 –

2

添加到您李娜的標籤這樣的預期標記菜單項:(例如用於飼料)

<li data-id="feed" class="pure-menu-selected"><a href="home.php">Feed</a></li> 

在你的CSS:

.pure-menu-selected[data-id=feed] { 
    background-color: #123456; 
} 
1
<script> 
$(document).ready(function(){ 
$('ul li').click(function(){ 
    $('ul li').removeClass('pure-menu-selected'); 
    $(this).addClass('pure-menu-selected'); 
    }); 
}); 
</script> 

的jsfiddle:http://jsfiddle.net/QCH3q/1/