jQuery——獲取並設定CSS類
removeClass():從被選元素刪除一個或多個類
toggleClass():對被選元素進行新增/刪除類的切換操作
css():設定或返回被選元素的一個或多個樣式屬性
樣式表例項:
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
addClass()
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.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>
可以在addClass()方法中規定多個類:
$("button").click(function(){
$("#div1").addClass("important blue");
});
removeClass()
<!DOCTYPE html><html>
<head>
<script src="/jquery/jquery-1.11.1.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>
jQuery toggleClass()
<html>
<head>
<script src="/jquery/jquery-1.11.1.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>
返回CSS屬性
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.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>
設定CSS屬性
css("propertyname","value");<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.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>設定CSS屬性</button>
</body>
</html>
設定多個CSS屬性
$("p").css({"background-color":"yellow","font-size":"200%"});jQuery——尺寸
width():設定或返回元素的寬度(不包括內邊距、邊框或外邊距)height():設定或返回元素的高度(不包括內邊距、邊框或外邊距)
innerWidth()方法返回元素的寬度(包括內邊距)
innerHeight()方法返回元素的高度(包括內邊距)
outerWidth()方法返回元素的寬度(包括內邊距和邊框)
outerHeight()方法返回元素的高度(包括內邊距和邊框)
outerWidth(true)方法返回元素的寬度(包括內邊距、邊框、外邊距)
outerHeight(true)方法返回元素的高度(包括內邊距、邊框、外邊距)
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.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>