简体   繁体   中英

css jquery changing class style

$("#menu li").click(function () {
        $("#menu .active").removeAttr("class");
        $(this).attr("class","active");
    });

        <div id="menu">
            <ul>
                <li id="m1" class="active"><a>link 1</a></li>
                <li id="m2"><a>link 1</a></li>
                <li id="m3"><a>link 2</a></li>
                <li id="m4"><a>link 3</a></li>
                <li id="m5"><a>link 4</a></li>
                <li id="m6"><a>link 5</a></li>
                <li id="m6"><a href="">link 6</a></li>
                <li id="m7"><a href="">link 7</a></li>
                <li id="m8"><a>link 8</a></li>
            </ul>
        </div>

css:

#menu .active{
    background:white;
}

#menu .active a{
    opacity:0.5;
}

#menu a{
    color:#08042b;
    text-decoration:none;
    font-size:14px;
    text-shadow: 1px 0px 1px #6055c8;
    position:relative;

}

When i click a link the first time everything seems to be ok.. When i click for second time background-color of this li changed li font takes opacity 0.5, previous li hasn't active className but previous li text hasn't go to opacity:1 Any help?

As it was said, using $("#menu .active").removeClass("active"); is more correct than removing the entire class attribute was you did, as you may ending losing other classes you may have, thus resulting in unexpected behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM