<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script> <style type="text/css"> .redbg{ background: red; } .active{ color: beige; } </style> </head> <body> <h1>helloworld</h1> <button>黑白切換</button> <script type="text/javascript"> $('h1').addClass('redbg') // 添加樣式 $('h1').addClass('active') // 刪除樣式 $('h1').removeClass('active') var isblack=true $('button').click(function(){ // if(isblack){ // $('h1').addClass('active') // isblack=false // }else{ // $('h1').removeClass('active') // isblack=true // } //切換樣式 $('h1').toggleClass('active') }) .css({ background:'red', color:'skyblue' }).html('這是一個按鈕') // $('button').css('background','red') // $('button').html('這是個可以切換的按鈕') </script> </body> </html>