我想为每个li使用php激活时添加类当前(我绝对是新手)。我有以下代码:
<ul id="frumos">
<li {if $pagename eq "upcoming"} class="current" {/if}><a href="/upcoming.php?category=economic">Economic</a></li>
<li {if $pagename eq "/upcoming.php?category=other"} *(this doesn't work)* class="current" {/if}><a href="/upcoming.php?category=other">Freestyle</a></li>
<li><a href="/upcoming.php?category=social">Social</a></li>
</ul>
如果我使用{if $pagename eq "upcoming"}
它可以正常工作但是如果我放{if $pagename eq "/upcoming.php?category=other"}
它不起作用,在这种情况下我应该使用什么结构?
答案 0 :(得分:1)
试试这个:
<?PHP
$category = $_REQUEST[category];
echo "<ul id='frumos'>";
echo "<li ";
if($category == 'economic'){
echo " class='current'";
}
echo "><a href='/upcoming.php?category=economic'>Economic</a></li> ";
echo "<li";
if($category == 'other'){
echo " class='current'";
}
echo "><a href='/upcoming.php?category=other'>Freestyle</a></li>";
echo "<li";
if($category == 'social'){
echo " class='current'";
}
echo "><a href='/upcoming.php?category=social'>Social</a></li>";
echo "</ul>";
?>
答案 1 :(得分:0)
屠杀你的HTML最简单,最干净的方法是使用turinary。基本上它是一个紧凑的if else声明。
<li <?php echo ($pagename === "/upcoming.php?category=other") ? echo 'class="current"' : ''; ?> >