点击span实现样式变化!求大神帮忙

1.点击span 使span的class属性发生变化
class="btn btn-large btn-primary"------ btn-primary这是引入样式

2.原代码

    <div id="myTab" class="pull-right">        <a href="#listView" data-toggle="tab">
            <span class="btn btn-large" ><i class="icon-list"></i></span></a> 
        <a href="#blockView" data-toggle="tab">            
        <span class="btn btn-large btn-primary"><i class="icon-th-large"> </i></span></a>
    </div>

3.目前写成这样就是不生效

    <jsp:include page="/static/common/cart_js.jsp"/>
    <script type="text/javascript">
    
         $("span").each(function(i){            if(i==0){
                $("#listView span").on("click",function(){
    
                    $(this).addClass("btn btn-large");
                    })
            }else{
                $("#blockView span").on("click",function(){
    
                    $(this).addClass("btn btn-large btn-primary");
                    })
            }
        }); 
    </script>

4.网页效果图

https://img3.mukewang.com/5bc93834000168dd01270049.jpg

求大神帮忙

慕丝7291255
浏览 959回答 1
1回答

慕码人8056858

&nbsp;$("span").each(function(i){ &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("#myTab&nbsp;span.btn").click(function(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("span").removeClass("btn-primary"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(this).addClass("btn-primary"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript