jQuery實現單擊某個標籤改變樣式

dreams7433發表於2018-12-27

1、HTML程式碼,如下圖:

<p class="sc_member_recharge_form">
     <span class="selected">20元</span>
     <span >50元</span>
     <span >100元</span>
     <span >200元</span>
     <span >300元</span>
     <span >其它金額</span>
</p>

2、jQuery程式碼,如下圖:

<script>
    $(document).ready(function(){
        $(".sc_member_recharge_form span").click(function(){
            var select = $(this).text();
            if(select === `other amount` || select === `其它金額`){
                $(".sc_member_recharge_form span").removeClass("selected");
                $(this).addClass("selected");
            }else{
                $(".sc_member_recharge_form span").removeClass("selected");
                $(this).addClass("selected");
            }

        });
    });
</script>

 

相關文章