如果链接处于活动状态,则保持悬停颜色


Making hover color stay if link is active

我正在开发一个使用PHP,HTML和CSS的网站。目前在我的导航栏中,我已将背景颜色设置为灰色。当用户将鼠标悬停在导航栏上时,每个链接都会获得蓝色背景,但是当我单击该链接时,背景颜色将恢复为灰色。如果该特定链接处于活动状态,如何使悬停颜色保持蓝色。

这是我的导航栏的代码。

<tr>
<td width="800" height="54">
<div id="nav" >
<ax><b><font face="Arial" ><a style="text-decoration: none; color:#303030" href="index.php" >HOME</a></font></b></ax>
<bx><b><font face="arial" ><a style="text-decoration: none; color:#303030" href="edituser.php?own=y">IT</a></font></b></bx>
<cx><b><font face="arial" ><a style="text-decoration: none; color:#303030" href="newsevents.php">HUMAN RESOURCE</a></font></b></cx>
<dx><b><font face="arial" ><a style="text-decoration: none; color:#303030" href="industries.php">PROCUREMENT</a></font></b></dx>
<ex><b><font face="arial" size="1"><a style="text-decoration: none; color:#303030" href="http://www.csmphilippines.com/aboutus.html">FINANCE</a></font></b></ex>
<fx><b><font face="arial" ><a style="text-decoration: none; color:#303030" href="hact.php">HACT</a></font></b></fx>
</div>
</td>

这是我的 CSS 的代码

#nav {
text-decoration:none;
padding-bottom:10px;
border-bottom:none;
width:
}
#nav ax {
display:inline;
padding:15px;
padding-left:31px;
padding-right:28px;
background-color:#ececec;
text-decoration:none;
}
#nav bx {
display:inline;
padding:15px;
padding-left:45px;
padding-right:45px;
background-color:#ececec;
text-decoration:none;
}
#nav cx {
display:inline;
padding:15px;
padding-left:45px;
padding-right:45px;
background-color:#ececec;
text-decoration:none;
}
#nav dx {
display:inline;
padding:15px;
padding-left:45px;
padding-right:45px;
background-color:#ececec;
text-decoration:none;
}
#nav ex {
display:inline;
padding:15px;
padding-left:53px;
padding-right:53px;
background-color:#ececec;
text-decoration:none;
}
#nav fx {
display:inline;
padding:15px;
padding-left:30px;
padding-right:30px;
background-color:#ececec;
text-decoration:none;
}
#nav ax:hover {
background-image:url(images/tabbackit.jpg); 
background-repeat:repeat-x;
height:900px;
}

#nav bx:hover {
background-image:url(images/tabbackit.jpg); 
background-repeat:repeat-x;
height:900px;
}
#nav cx:hover {
background-image:url(images/tabback.jpg); 
background-repeat:repeat-x;
height:900px;
}
#nav dx:hover {
background-image:url(images/tabbackproc.jpg); 
background-repeat:repeat-x;
height:900px;
}
#nav ex:hover {
background-image:url(images/tabbackfin.jpg); 
background-repeat:repeat-x;
height:900px;
}
#nav fx:hover {
background-image:url(images/tabbackhact.jpg); 
background-repeat:repeat-x;
height:900px;
}

我这样做的方法是在正文标签中为每个页面提供自己的 id:

<body id="pgAbout">

然后为每个菜单项设置一个单独的 ID:

<li id="mnuAbout">

使用 CSS,您现在可以为您所在的页面设置链接的样式:

#pgAbout #mnuAbout,
#pgOther #mnuOther
{
   /* Styles here */
}

这是一个简短的演示

更新

这是另一个使用您自己的HTML的演示,以阐明它是如何工作的。

您可以做的一件简单的事情是对所有页面使用相同的样式表,但使用不同的 CSS 类来突出显示每个页面上的相应链接。例如,在 index.php 上,使用 .active 类突出显示"主页"链接。

既然你是新手(如你所说),让我们分解一下:

对所有页面使用相同的 CSS。但是,为导航栏元素定义两个 CSS 类:一个是.regular状态,另一个是.active状态。在每个页面中,为与该页面对应的链接提供活动类,而其他链接保留在常规类中。

但是,您希望它保持链接吗?

很简单,只需输入以下代码例如在主页上,将以下代码放在 HEAD 中

<style type="text/css">
    #nav ax{
        background-image:url(images/tabbackit.jpg); 
        background-repeat:repeat-x;
    }
</style>