jquery中修改css樣式的方法:1、使用“css("屬性名","屬性值")”語句修改;2、使用“css({屬性名:"屬性值",屬性名:"屬性值"…})”語句修改;3、使用“toggleClass(“類名”)”語句修改。
本教程操作環境:windows7系統、jquery1.10.0版本、Dell G3電腦。
JQuery修改css樣式的方法
css部分:
.div1{ width:100px; height:100px; background:#00ff00; } .div2{ width:100px; height:100px; background:#ff0000; }#Btndiv{ width:100px; height:100px; border: 1px solid #ccc; margin-bottom: 10px; }
jQuery代碼:
<div id="Btndiv" onclick="changeCss()"></div> <script> $(document).ready(function (){ //第一種 // $("div").css("width","100px"); // $("div").css("height","100px"); // $("div").css("background","cyan"); //第二種 // $("div").css({ // width:"100px",height:"100px",background:"red" // }); //第三種 $("div").addClass("div1"); $("div").click(function (){ // $(this).addClass("div2"); // $(this).removeClass("div1"); $(this).toggleClass("div2"); }); }); </script>
相關視頻教程推薦:jQuery教程(視頻)