jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作詳解
本文實(shí)例講述了jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作。分享給大家供大家參考,具體如下:
- addClass():向被選元素添加一個(gè)或多個(gè)類
- removeClass():從被選元素刪除一個(gè)或多個(gè)類
- toggleClass():對(duì)被選元素進(jìn)行添加/刪除類的切換操作
- css():設(shè)置或返回被選元素的一個(gè)或多個(gè)樣式屬性
樣式表實(shí)例:
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
addClass()
<!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");
});
removeClass()
<!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é)果:

toggleClass()
<!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()
返回CSS屬性
css("propertyname")
<!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">紅花盛開(kāi)</p>
<p id=p3 style="background-color:#0000ff">玲瓏剔透</p>
<button>css()</button>
</body>
</html>
運(yùn)行結(jié)果:

設(shè)置CSS屬性
css("propertyname","value");
<!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屬性
$("p").css({"background-color":"yellow","font-size":"200%"});
jQuery——尺寸
- width():設(shè)置或返回元素的寬度(不包括內(nèi)邊距、邊框或外邊距)
- height():設(shè)置或返回元素的高度(不包括內(nèi)邊距、邊框或外邊距)
- innerWidth()方法返回元素的寬度(包括內(nèi)邊距)
- innerHeight()方法返回元素的高度(包括內(nèi)邊距)
- outerWidth()方法返回元素的寬度(包括內(nèi)邊距和邊框)
- outerHeight()方法返回元素的高度(包括內(nèi)邊距和邊框)
- outerWidth(true)方法返回元素的寬度(包括內(nèi)邊距、邊框、外邊距)
- outerHeight(true)方法返回元素的高度(包括內(nèi)邊距、邊框、外邊距)
<!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.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容還可查看本站專題:《jQuery切換特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery驗(yàn)證郵箱格式是否正確實(shí)例講解
這篇文章主要介紹了一個(gè)jquery驗(yàn)證郵箱格式是否正確的實(shí)踐案例,利用正則表達(dá)式進(jìn)行驗(yàn)證,感興趣的小伙伴們可以參考一下2015-11-11
如何實(shí)現(xiàn)星星評(píng)價(jià)(jquery.raty.js插件)
本文主要分享了用jQuery插件jquery.raty.js實(shí)現(xiàn)星星評(píng)價(jià)功能:后臺(tái)傳數(shù)據(jù),前臺(tái)顯示星星個(gè)數(shù)的具體方法。有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
jQuery實(shí)現(xiàn)可高亮顯示的二級(jí)CSS菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)可高亮顯示的二級(jí)CSS菜單效果,涉及基本的jquery鼠標(biāo)事件及頁(yè)面元素樣式動(dòng)態(tài)改變技巧,需要的朋友可以參考下2015-09-09
運(yùn)用jQuery寫的驗(yàn)證表單(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇運(yùn)用jQuery寫的驗(yàn)證表單(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
jQuery實(shí)現(xiàn)仿百度帖吧頭部固定導(dǎo)航效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿百度帖吧頭部固定導(dǎo)航效果,涉及jquery針對(duì)頁(yè)面高度計(jì)算與樣式的動(dòng)態(tài)添加及刪除技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08

