html页面多个a标签点击时显示不同的样式

时间:2024-08-17 16:03:14
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <style type="text/css">
    .orderSelect{color:pink;}
    </style>
</head>
<body>
    <div class="order-box clearfix">
            <div class="order-wrap ">
                <a href="test.html?a=1" >全部订单</a>
            </div>
            <div class="order-wrap">
                <a href="test.html?a=2">待服务</a>
            </div>
            <div class="order-wrap">
                <a href="test.html?a=3">已完结</a>
            </div>
            <div class="order-wrap">
                <a href="test.html?a=4">已取消</a>
            </div>
    </div>
</body>
<script type="text/javascript">
    $(function(){
        /*判断访问的路径和当前a标签的路径,如果一致,就添加需要的样式*/
        $('.order-box .order-wrap').each(function(){
            if($($(this)).find("a")[0].href==String(window.location))
            $(this).find("a").addClass('orderSelect');
        });

    });
</script>
</html>