使用css更改所选菜单项的颜色

时间:2015-02-15 11:28:56

标签: javascript html css html5 css3

我想使用css样式突出显示当前打开的页面。这是我的HTML代码

<ul class="nav" id="nav">
  <li id="home">
    <a href="home.html" data-id="home" target="ifrm">Home</a>
    <ul>
     <li><a href="item1.html" target="ifrm">item1</a></li>
     <li><a href="item2.html" target="ifrm">item2</a></li>
     <li><a href="item3.html" target="ifrm">item3</a></li>
    </ul>
  </li>
  <li id="skill"><a href="skill.html" data-id="skill" target="ifrm">Skill</a></li>
  <li id="research"><a href="research.html" data-id="research"    target="ifrm">Research</a></li>
  <li id="link"><a href="link.html" data-id="link" target="ifrm">Link</a></li>
</ul>
<iframe name="ifrm"></iframe>

应该只更改菜单列表的字体和背景颜色,在iframe中加载女巫页面。

2 个答案:

答案 0 :(得分:4)

&#13;
&#13;
$(function(){
   $('li a').click(function(){
     $('li a').each(function(a){
       $( this ).removeClass('selectedclass')
     });
     $( this ).addClass('selectedclass');
   });
  
  $('ul a').click(function(){
     $('ul a').each(function(a){
      $( this ).removeClass('selectedclass')
     });
     $( this ).addClass('selectedclass');
   });
});
&#13;
li a.selectedclass
{
  color:red;
  background-color:blue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav" id="nav">
          <li id="home"><a href="home.html" data-id="home" target="ifrm">Home</a>
		  <ul>
						<li ><a href="item1.html" target="ifrm">item1</a></li>
						<li ><a	href="item2.html" target="ifrm">item2</a></li>
						<li ><a href="item3.html" target="ifrm">item3</a></li>

		  </ul>
		  
		  </li>
          <li id="skill"><a href="skill.html" data-id="skill" target="ifrm">Skill</a></li>
          <li id="research"><a href="research.html" data-id="research"    target="ifrm">Research</a></li>
          <li id="link"><a href="link.html" data-id="link" target="ifrm">Link</a></li>
        </ul>

<iframe name="ifrm"></iframe>
&#13;
&#13;
&#13;

首先需要定义一个具有所需样式的类,然后单击锚标记从所有类中删除该类,然后将其添加到单击的锚标记中。

答案 1 :(得分:0)

在您的文件中添加此代码

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $("a").click(function(){
        $("a").css("color","BLUE"); // change this color to default one
        $( this ).css( "color", "RED" ); 
    });
</script>