菜单栏点击后高亮

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>菜单栏选中高亮</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.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>
</head>
<body>
<ul class="menu" id="menu">
  <li><a href="#">首页</a></li>
  <li><a href="#">PHP综合</a></li>
  <li><a href="#">Ecshop</a></li>
</ul>
<script type="text/javascript">

  var urlstr = location.href;
  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>
</body>
</html>

ps:主要思想遍历DOM 树

posted @ 2017-07-21 11:33  pengcx  阅读(332)  评论(0编辑  收藏  举报