鍍金池/ 問(wèn)答/HTML/ class樣式切換 一定要用標(biāo)記才能切換嗎?

class樣式切換 一定要用標(biāo)記才能切換嗎?

<button type="button" class="btn btn-primary " >庫(kù)存 <span class="glyphicon glyphicon-sort-by-attributes-alt"></button>

想對(duì)class='glyphicon glyphicon-sort-by-attributes-alt'進(jìn)行切換為class='glyphicon glyphicon-sort-by-attributes' (預(yù)期實(shí)現(xiàn):點(diǎn)擊按鈕兩種樣式切換)
直接用toggleClass可以實(shí)現(xiàn)嗎?

回答
編輯回答
生性

兩種class名可以用下面的代碼:

if($('button').hasClass('glyphicon-sort-by-attributes-alt')){
    $('button').add('glyphicon-sort-by-attributes').removeClass('glyphicon-sort-by-attributes-alt')
}
else {
    $('button').add('glyphicon-sort-by-attributes-alt').removeClass('glyphicon-sort-by-attributes')
}
2017年11月18日 20:21
編輯回答
默念

可以啊,即 classNameOne 是一個(gè)默認(rèn)樣式,如果點(diǎn)擊,使用 toggleClassTwo 則根據(jù)情況刪除或添加類名,然后類名根據(jù)權(quán)重再跟換樣式就可以了。然后多嘴一句,這種事情,自己試一下就知道了。

2017年8月11日 08:27