如何保持选中的列表项

时间:2016-09-26 13:11:15

标签: html css

是否可以将颜色添加到具有链接的所选列表项?

我的代码:

.grid a:active {
  background-color: yellow;
}
<li class="grid">
  <a href="modulo_ordine1.php">Modulo d'ordine</a>
</li>

悬停时黄色会停留。

我希望在访问列表项后始终将颜色黄色保留在列表项中,因此如果选择它直到选择了另一个列表项。因此,如果我有一些列表项,我希望选择的那个列为黄色,直到选择另一个。

这可能吗?

2 个答案:

答案 0 :(得分:0)

试试这个

jQuery('.grid a').click(function(){
   jQuery(this).toggleClass('active');
});

答案 1 :(得分:0)

function a(yellow){
  if(yellow=="link1"){
document.getElementById("test").style.background="yellow"
document.getElementById("test1").style.background="none"
    
  }
  else if(yellow=="link2"){
document.getElementById("test").style.background="none"
document.getElementById("test1").style.background="yellow"
    
  }
}
 #test{
 background:yellow; 
  
}
  <li class="grid">
  <a id="test"  href="#"  onclick="a('link1')">Modulo d'ordine</a>
  
    <a id="test1"  href="#" onclick="a('link2')">Modulo d'ordine1</a>

					</li>

相关问题