最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作詳解

 更新時(shí)間:2018年09月05日 10:19:16   作者:furway  
這篇文章主要介紹了jQuery實(shí)現(xiàn)獲取及設(shè)置CSS樣式操作,結(jié)合實(shí)例形式分析了jQuery中常用的addClass()、removeClass()、toggleClass()及css()相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下

本文實(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">紅花盛開</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常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

达日县| 商洛市| 微山县| 安远县| 邮箱| 绥江县| 河间市| 清新县| 江山市| 即墨市| 溆浦县| 陆川县| 大新县| 富顺县| 石门县| 申扎县| 牙克石市| 苏州市| 林口县| 桦甸市| 城市| 杭州市| 沽源县| 吐鲁番市| 波密县| 察隅县| 怀化市| 铁岭县| 伊宁市| 万源市| 蚌埠市| 闵行区| 长武县| 焦作市| 河津市| 双流县| 万荣县| 平乐县| 历史| 新民市| 宁明县|