JS addclass为什么有的可以,有的不可以,新手请多指教,感激不尽

 <script src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".nav a").each(function () {
            $this = $(this);
            if ($this[0].href == String(window.location)) {
                $this.addClass("hover");
            }
        });
    });
</script>
<style>
.hover A:visited{color:#FFF !important;  background-color:#b9332a;}
.hover{background-color:#b9332a; border-radius: 8px; color:#FFF !important;}
.nav-collapse{width: 410px;border-bottom:solid 1px #770801;margin:0px auto 0 auto;}
.nav-collapse ul li{background-color:#b9332a; /*border:solid 1px #6c0801;*/ border-bottom:0;}
.nav-collapse ul li a{padding-left: 10px;color: #fff; font-size:12px; display: block; font-weight:bold; /*height:36px;*/line-height: 36px;position: relative;}
.nav-collapse ul li ul{display: none;}
.nav-collapse ul li ul li ul{display: none;}
.nav-collapse ul li ul li { background-color:#fae7ec; border-color:#d9655d; }
.last{ background-color:#d6e6f1; border-color:#6196bb; }
.nav-collapse ul li ul li  a{ color:#b9332a; padding-left:30px;}
.js .nav-collapse-0.opened {max-height: 621px !important;}
</style>

<body>
        <DIV class="nav">
            <UL class="navList">
              <LI class="default"><A href="index.aspx">首页</A></LI>
              <LI class="default"><A href="view.aspx?id=1">公司概况</A></LI>
              <LI class="default"><A href="list.aspx?pid=63">新闻动态</A></LI>
              <LI class="default"><A href="lis.aspx?pid=71">产品与解决方案</A></LI>
              <LI class="default"><A href="view.aspx?id=2">联系我们</A></LI>
              <LI class="default"><A href="list.aspx?pid=69">客户服务</A></LI>
            </UL>
        </DIV>
</body>

是你的 if ($this[0].href == String(window.location)) {这个判断不成立,和jquery没关系,自己alert下就知道了。。

if (this.href == window.location.href) {
        $this.addClass("hover");
}

$this[0].href这个写法有问题,而且建议变量不要用$关键字,可以直接用$(this).href操作就是撒