【技術】點擊後變色(jQuery的removeClass和addClass)

來源:互聯網
上載者:User

標籤:style   color   java   使用   io   ar   cti   代碼   sp   

執行個體見銳意先行《手機test》中《手機test5》。

針對a寫兩段樣式:

.nav_ul > li > a {
    display: block;
    background: url(‘images/down.png‘) no-repeat scroll right center transparent;
    padding: 10px 0px;
    border-bottom: 1px solid #E2E2E2;
    color: #000;
    font-size: 1.5em;
    transition: all 0.4s ease 0s;
}

/*點擊前的color是#000;*/
.nav_ul > li > a.color{
    color: #E60012;
}

/*點擊後的color是#E60012;*/

JS代碼:

<script type="text/javascript">

$(document).ready(function(){
    $(‘#js_nav_ul li a‘).click(function(){
        $(‘#js_nav_ul li > a‘).removeClass(‘color‘);
        $(this).addClass(‘color‘);
    });
});
</script>

//removeClass() 方法從被選元素移除一個或多個類。

addClass() 方法向被選元素添加一個或多個類。

該方法不會移除已存在的 class 屬性,僅僅添加一個或多個 class 屬性。

提示:如需添加多個類,請使用空格分隔類名。

 

代碼先寫removeClass() ,去掉原先的樣式color: #000;

後寫addClass(),追加新樣式 color: #E60012;。

 

【技術】點擊後變色(jQuery的removeClass和addClass)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.