2016-12-31 76 views
0

有人可以幫我用橫向菜單嗎?我想在此菜單上創建活動頁面,我需要向css文件添加新類。我希望你能理解我。由於如何使現有的水平菜單處於活動狀態?


這是我現在有:menu-theme2.css

<ul class="theme2"> 
     <li><a href="index.php"><span>HOME</span></a></li> 
     <li><a href="kredity.php"><span>KREDITY</span></a></li> 
     <li><a href="shop.php"><span>SHOP</span></a></li> 
     <li><a href="aukce.php"><span>AUKCE</span></a></li> 
     <li><a href="servery.php"><span>SERVERY</span></a> 
     <li><a href="forum"><span>FORUM</span></a></li> 
    </ul> 


,我想這對index.php文件:

 <ul class="theme2"> 
     <li><a class="active" href="index.php"><span>HOME</span></a></li> 
     <li><a href="kredity.php"><span>KREDITY</span></a></li> 
     <li><a href="shop.php"><span>SHOP</span></a></li> 
     <li><a href="aukce.php"><span>AUKCE</span></a></li> 
     <li><a href="servery.php"><span>SERVERY</span></a> 
     <li><a href="forum"><span>FORUM</span></a></li> 
    </ul> 

回答

0

嘗試這樣的事情。

對於水平菜單,您可以將css規則放在您的css文件中。

.theme2 li { 
    float: left; 
    list-style: outside none none; 
    margin-left: 10px; 
} 

對於活動類。

$(function(){ 
    var url = window.location.pathname; 
    alert(url); 
    urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); 
    // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there 
    // now grab every link from the navigation 
    $('.theme2 ul li a').each(function(){ 
    // and test its normalized href against the url pathname regexp 
     if(urlRegExp.test(this.href.replace(/\/$/,''))){ 
      $(this).addClass('active'); 
     } 
    }); 
    }); 
+0

這裏,我必須把這個功能? index.php中的某處? http://pastebin.com/52sH7BJ1 – Seuss

+0

我不能使用簡單的代碼呢? li a:hover:not(.active){background_color:#111; } .active {background_color:#4CAF50; } – Seuss

0
index.php 
<?php $page = "index"; ?> 
shop.php 
<?php $page = "shop"; ?> 
<ul> 
<li><a <?php if($page == 'index') {echo 'class="active"';} ?> href="index.php"><span>HOME</span></a></li> 

<li><a <?php if($page == 'shop') {echo 'class="active"';} ?> href="index.php"><span>SHOP</span></a></li> 
</ul> 
0

繼Nikit的解決方案,你可以做以下的無需在每個頁面設置$page變量值,使用PHP的__FILE__常數:

<ul> 
<li><a <?php if(basename(__FILE__, '.php') == 'index') {echo 'class="active"';} ?> href="index.php"><span>HOME</span></a></li> 

<li><a <?php if(basename(__FILE__, '.php')== 'shop') {echo 'class="active"';} ?> href="index.php"><span>SHOP</span></a></li> 
</ul> 
相關問題