繁体   English   中英

如何在CSS代码中更改属性背景颜色

[英]How to change the Attribute background color in css code

我使用了以下代码,当我单击下一个菜单时,我想替换旧的颜色。

请给我解决方案。 当我单击下一个选项时,如何替换旧颜色。

HTML代码:

    <head>
<title>Untitled Document</title>
</head>
<body>

<script type="text/javascript">
      function ChangeColor(obj) {
         obj.style.backgroundColor = "#bfcbd6";
      }
</script>

<head>
<link rel="stylesheet" href="css/style.css" type="text/css">
<ul id="css3menu1" class="topmenu">
    <li class="topmenu"><a onclick="ChangeColor(this);"  href="#" title="Home" style="width:154px;" >Home <span class="numberend">1</span> </a></li>
    <li class="topmenu"><a onclick="ChangeColor(this);" href="#" title="Product info" style="width:154px;"><span>Product info</span></a>
    </li>
</ul>
</div>   
</body>
</html>

请检查以下链接: http : //jsfiddle.net/8JwhZ/1058/

演示

function ChangeColor(obj) {
    /* Set all the link colors back to the original one */
    var links = document.querySelectorAll('.topmenu a');
    for (i = 0; i < links.length; i++) {
        links[i].style.backgroundColor = "#005984";
    }
    /* Set the color of the selected one */
    obj.style.backgroundColor = "#bfcbd6";
}

如果我了解您的帖子,那么您对jsfidle会有所了解。

您可以使用如下所示的内容:

  </head>
  <script>
   $('.menu').click(
      function(){
         $(this).css("background","#bfcbd6")
      }
    )

  </script>
  <body>
  <link rel="stylesheet" href="css/style.css" type="text/css">
    <ul id="css3menu1" class="topmenu">
      <li class="topmenu"><a href="#" title="Home" class="menu" style="width:154px;" >Home <span class="numberend">1</span> </a></li>
      <li class="topmenu"><a href="#" class="menu" title="Product info" style="width:154px;"><span>Product info</span></a>
      </li>
    </ul>

</body>

http://jsfiddle.net/7F7BP/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM