本文實(shí)例講述了jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作。分享給大家供大家參考,具體如下:
樣式表實(shí)例:
.important{font-weight:bold;font-size:xx-large;}.blue{color:blue;}<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("h1,h2,p").addClass("blue");$("div").addClass("important");});});</script><style type="text/css">.important{font-weight:bold;font-size:xx-large;}.blue{color:blue;}</style></head><body><h1>she is gone</h1><h2>no say anymore</h2><p>but i miss you</p><div>can you come back</div></br><button>addClass</button></body></html>運(yùn)行效果:

可以在addClass()方法中規(guī)定多個(gè)類:
$("button").click(function(){$("#div1").addClass("important blue");});<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("h1,h2,p").removeClass("blue");});});</script><style type="text/css">.important{font-weight:bold;font-size:xx-large;}.blue{color:blue;}</style></head><body><h1 class="blue">is she ?</h1><h2 class="blue">i do not know</h2><p class="blue">queit</p><br><button>removeClass</button></body></html>運(yùn)行結(jié)果:

<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/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>who are you</h1><h2> i five thank you</h2><p>goodbye</p><button>toggleClass</button></body></html>運(yùn)行結(jié)果:

返回CSS屬性
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){alert("Background color = " + $("#p2").css("background-color"));});});</script></head><body><h2>我心在南朝</h2><p id=p1 style="background-color:#ff0000">田野上</p><p id=p2 style="background-color:#00ff00">紅花盛開</p><p id=p3 style="background-color:#0000ff">玲瓏剔透</p><button>css()</button></body></html>運(yùn)行結(jié)果:

設(shè)置CSS屬性
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").css("background-color","yellow");});});</script></head><body><h2>我心在南朝</h2><p style="background-color:#ff0000">田野上</p><p style="background-color:#00ff00">紅彤彤的花兒</p><p style="background-color:#0000ff">玲瓏剔透</p><p>我心在南朝</p><button>設(shè)置CSS屬性</button></body></html>運(yùn)行結(jié)果:

設(shè)置多個(gè)CSS屬性
jQuery――尺寸
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){var txt="";txt+="Width of div: " + $("#div1").width() + "</br>";txt+="Height of div: " + $("#div1").height() + "</br>";txt+="innerWidth of div: " + $("#div1").innerWidth() + "</br>";txt+="innerHeight of div: " + $("#div1").innerHeight() + "</br>"txt+="Outer width: " + $("#div1").outerWidth() + "</br>"txt+="Outter height: " + $("#div1").outerHeight() + "</br>";txt+="Outer width: " + $("#div1").outerWidth(true) + "</br>"txt+="Outter height: " + $("#div1").outerHeight(true);$("#div1").html(txt);});});</script></head><body><div id="div1" style="height:150px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div><br><button>顯示div尺寸</button></body></html>運(yùn)行結(jié)果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容還可查看本站專題:《jQuery切換特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答