有人可以用水平菜单帮我吗?我想在这个菜单上创建acive页面,我需要为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 :(得分: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');
}
});
});
答案 1 :(得分: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>
答案 2 :(得分:0)
根据Nikit的解决方案,您可以使用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>
变量值的情况下执行以下操作:
{{1}}