Php 如何赋予活动菜单项不同的样式
我的菜单是一组链接,然后我使用CSS将其设置为按钮样式Php 如何赋予活动菜单项不同的样式,php,javascript,jquery,css,Php,Javascript,Jquery,Css,我的菜单是一组链接,然后我使用CSS将其设置为按钮样式 <div id="menu"> <a href="" class="mybutton">Item 1</a> <a href="" class="mybutton">Item 3</a> <a href="" class="mybutton">Item 3</a> </div> 当一个菜单项被点击并处于活动状态时,最好的方
<div id="menu">
<a href="" class="mybutton">Item 1</a>
<a href="" class="mybutton">Item 3</a>
<a href="" class="mybutton">Item 3</a>
</div>
当一个菜单项被点击并处于活动状态时,最好的方式是什么?我是否使用jquery或javascript添加新类?或者有一个CSS技巧可以做到这一点?CSS的
:可以使用active
伪类:
a.mybutton:active {
/* rules */
}
CSS技巧是
相同:悬停和:已访问
祝你好运
编辑
既然你希望链接到你所在页面的样式不同,我需要更多的细节。你使用PHP吗?你不是每页使用一个php脚本吗
无论如何,如果您在所有页面中都包含一个header.php文件,或者您只是懒得硬编码每个链接的类,那么这应该是可行的
PHP:
CSS
你的档案
<!-- Simply echo the function result for each link class -->
<a href="home.php" class="<?php echo is_current('home.php','active'); ?>">Home</a>
<a href="about.php" class="<?php echo is_current('about.php','active'); ?>">About</a>
假设您的意思是“当用户查看与链接对应的页面时”(与“当用户在链接上按下鼠标按钮但尚未释放它时”相反):
在链接中包括一个类(例如
current
),然后在选择器中使用它。在将类提供给用户之前,将其添加到页面的HTML中,让服务器端进程这样做通常是最好的方法。您能给我一个简单的代码示例吗?我是否用JQuery添加了当前的类,以及我还需要如何在css中使用类似#menu a:active{}
的东西?正如我所说的,应该在服务器端完成。具体取决于您使用的服务器端语言。选择器应该更符合#菜单a.current
。我现在就知道了。我也在想同样的事情,一旦点击实际指向该页面,活动状态就不会持续。那么我如何有条件地用PHP添加当前的类呢?
// Return $return if this page is $page, false otherwise
function is_current($page, $return) {
$this_page = $_SERVER['SCRIPT_NAME']; // will return /path/to/file.php
$bits = explode('/',$this_page);
$this_page = $bits[count($bits)-1]; // will return file.php, with parameters if case, like file.php?id=2
$bits = explode('?',$this_page);
$this_script = $bits[0]; // will return file.php, no parameters
return ($page == $this_script?$return:false); // return $return if this is $page, false otherwise
}
/* blue, no underline when normal */
a {
text-decoration: none;
color: #00f;
}
/* red, underlined when class active */
a.active {
text-decoration: underline;
color: #f00;
}
<!-- Simply echo the function result for each link class -->
<a href="home.php" class="<?php echo is_current('home.php','active'); ?>">Home</a>
<a href="about.php" class="<?php echo is_current('about.php','active'); ?>">About</a>