js实现导航菜单点击切换选中时高亮状态


<script type="text/javascript" src="http://www.daixiaorui.com/Public/js/jquery.min.js"></script>

<style>

.menu { padding:0; margin:0; list-style-type:none;}

.menu li { background:#FFD1A4; margin-right:1px; float:left; color:#fff; }

.menu li a { display:block; width:80px; text-align:center; height:32px; line-height:32px; color:#fff; font-size:13px; text-decoration:none;}

.cur{ background:#D96C00; font-weight:bold;}

</style>

 

<ul class="menu" id="menu">

  <li><a href="demo1.html?aa=1">首页</a></li>

  <li><a href="demo1.html?aa=2">栏目一</a></li>

  <li><a href="demo1.html?aa=3">栏目二</a></li>

</ul>

 

<script type="text/javascript">

  var urlstr = location.href;

  //alert(urlstr);

  var urlstatus=false;

  $("#menu  a").each(function () {

    if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') {

      $(this).addClass('cur'); urlstatus = true;

    } else {

      $(this).removeClass('cur');

    }

  });

  if (!urlstatus) {$("#menu a").eq(0).addClass('cur'); }


</script>


image


微信扫码咨询
建站咨询
cache
Processed in 0.008945 Second.