在style.css中定義了垂直菜單。一旦用戶選擇特定的菜單項,我想激活樣式「活動」。我不能把class="active"
放在<a
之內,因爲那時這個物品會一直有效。我想要的是一旦選擇了該項目,就應用樣式「主動」。垂直菜單中的活動項目
<ul id = "vertmenu">
<li>
<img style='float: left; margin-right: 5px;' src='images/mailIcon.png'/>
<a href="#" onclick="change_page('tables/111.php', 'col2')"><span>111</span></a>
</li>
<li>
<img style='float: left; margin-right: 5px;' src='images/calendarIcon.png'/>
<a href="#" onclick="change_page('tables/222.php', 'col2')"><span>222</span></a>
</li>
<li>
<img style='float: left; margin-right: 5px;' src='images/contactsIcon.png'/>
<a href="#" onclick="change_page('tables/333.php', 'col2')"><span>333</span></a>
</li>
<li>
<img style='float: left; margin-right: 5px;' src='images/contactsIcon.png'/>
<a href="#" onclick="change_page('tables/444.php', 'col2')"><span>444</span></a>
</li>
</ul>
<!-- --------- CSS ----------- -->
ul#vertmenu li a.active, ul#vertmenu li a.focus {
background: none repeat scroll 0 0 #FFCF8B;
outline-style: none;
color: #AA0000;
font-weight: bold;
text-decoration: underline;
}
#vertmenu .active{
background: none repeat scroll 0 0 #FFCF8B;
outline-style: none;
color: #AA0000;
font-weight: bold;
text-decoration: underline;
}
UPDATE 2;
<head>
<script>
var links = document.getElementById("vertmenu").getElementsByTagName("a");
for(var i = 0; i < links.length; i++){
links[i].onclick = function(event){
for(var j = 0; j < links.length; j++){
links[j].classList.remove('active');
}
event.target.parentNode.classList.add('active');
change_page('tables/'+event.target.innerHTML+'.php','col2');
return false;
};
}
</script>
</head>
我們可以使用jQuery? – ShibinRagh