jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作詳解
本文實(shí)例講述了jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作。分享給大家供大家參考,具體如下:
- addClass():向被選元素添加一個或多個類
- removeClass():從被選元素刪除一個或多個類
- toggleClass():對被選元素進(jìn)行添加/刪除類的切換操作
- css():設(shè)置或返回被選元素的一個或多個樣式屬性
樣式表實(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ī)定多個類:
$("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">紅花盛開</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è)置多個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測試上述代碼運(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è)計有所幫助。
相關(guān)文章
判斷jQuery是否加載完成,沒完成繼續(xù)判斷的解決方法
下面小編就為大家分享一篇判斷jQuery是否加載完成,如果沒完成繼續(xù)判斷的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
基于jQuery實(shí)現(xiàn)的無刷新表格分頁實(shí)例
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)的無刷新表格分頁方法,結(jié)合完整實(shí)例形式較為詳細(xì)的分析了jQuery實(shí)現(xiàn)無刷新表格分頁的具體步驟與相關(guān)實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-02-02
有關(guān)于eclipse配置spket需要注意的一些地方
用eclipse開發(fā)jquery程序,可以安裝spket插件,這樣在寫代碼的時候,就會有智能感知.eclipse配置spket的文章網(wǎng)上到處都是,spket官網(wǎng)上也有介紹.但配置后有的人無論如何也沒有智能感知提示,我就是其中一個.2013-04-04
jquery清空input標(biāo)簽的值及清除標(biāo)簽里面的內(nèi)容
這篇文章主要介紹了jquery清空input標(biāo)簽的值,清除標(biāo)簽里面的內(nèi)容,清除input標(biāo)簽的值,可以通過直接將input標(biāo)簽的值設(shè)置為空來實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10
jQuery實(shí)現(xiàn)textarea自動增長寬高的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)textarea自動增長寬高的方法,涉及jQuery針對鍵盤按鍵的響應(yīng)及頁面元素的動態(tài)操作技巧,需要的朋友可以參考下2015-12-12
jquery validation插件表單驗(yàn)證的一個例子
jquery.validate.js validation表單的驗(yàn)證插件2010-03-03

