本文實例講述了jQuery使用toggleClass方法動態添加刪除Class樣式的方法。分享給大家供大家參考。具體分析如下:
jQuery通過toggleClass方法動態添加刪除Class,一次執行相當于addClass,再次執行相當于removeClass,運行下面的代碼點擊按鈕可以看到文本段落字體在藍色和黑色間切換
<!DOCTYPE html><html><head><script src="js/jquery.min.js"></script><script>$(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").toggleClass("blue"); });});</script><style type="text/css">.blue{color:blue;}</style></head><body><h1>Heading 1</h1><h2>Heading 2</h2><p>This is a paragraph.</p><p>This is another paragraph.</p><button>Toggle class</button></body></html>
希望本文所述對大家的jQuery程序設計有所幫助。
新聞熱點
疑難解答