JavaScript(21)jQuery - 获取并设置 CSS 类、尺寸

本文介绍如何使用 jQuery 进行 CSS 类的添加、删除和切换操作,并演示了如何利用 jQuery 设置或获取元素的样式属性。此外,还介绍了 jQuery 中处理元素尺寸的方法。
jQuery - 获取并设置 CSS 类
通过 jQuery,可以很容易地对 CSS 元素进行操作。

jQuery 拥有若干进行 CSS 操作的方法:
  • addClass() - 向被选元素添加一个或多个类
  • removeClass() - 从被选元素删除一个或多个类
  • toggleClass() - 对被选元素进行添加/删除类的切换操作
  • css() - 设置或返回样式属性

下面的样式表将用于本页的所有例子:
.important
{
font-weight:bold;
font-size:xx-large;
}

.blue
{
color:blue;
}

jQuery addClass() 方法
向不同的元素添加 class 属性。在添加类时,也可以选取多个元素:
$("button").click(function(){
  $("<span style="background-color: rgb(255, 255, 204);">h1,h2,p</span>").addClass("blue");
  $("div").addClass("important");
});

也可以在 addClass() 方法中规定多个类:
$("button").click(function(){
  $("#div1").addClass("<span style="background-color: rgb(255, 255, 204);">important blue</span>");
});

jQuery removeClass() 方法
如何在不同的元素中删除指定的 class 属性:
$("button").click(function(){
  $("<span style="background-color: rgb(255, 255, 204);">h1,h2,p</span>").removeClass("blue");
});

jQuery toggleClass() 方法
对被选元素进行添加/删除类的切换操作:
$("button").click(function(){
  $("h1,h2,p").toggleClass("blue");
});

jQuery css() 方法
css() 方法设置或返回被选元素的一个或多个样式属性。

返回 CSS 属性
返回首个匹配元素的 background-color 值:
$("p").css("background-color");
只能返回首个匹配元素的CSS属性。看来写代码要养成有id或是class的好习惯。。。。。。

设置 CSS 属性
为所有匹配元素设置 background-color 值:
$("p").css("background-color","yellow");
注意,这个可以为所有匹配元素设置CSS属性。

设置多个 CSS 属性
为所有匹配元素设置 background-color 和 font-size:
$("p").css({"background-color":"yellow","font-size":"200%"});


jQuery - 尺寸
通过 jQuery,很容易处理元素和浏览器窗口的尺寸。

jQuery 尺寸 方法:
  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()

jQuery width() 和 height() 方法
width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。
下面的例子返回指定的 <div> 元素的宽度和高度:
$("button").click(function(){
  var txt="";
  txt+="Width: " + $("#div1").width() + "</br>";
  txt+="Height: " + $("#div1").height();
  $("#div1").html(txt);
});

jQuery innerWidth() 和 innerHeight() 方法
innerWidth() 方法返回元素的宽度(包括内边距)。
innerHeight() 方法返回元素的高度(包括内边距)。
下面的例子返回指定的 <div> 元素的 inner-width/height:
$("button").click(function(){
  var txt="";
  txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
  txt+="Inner height: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});

jQuery outerWidth() 和 outerHeight() 方法
outerWidth() 方法返回元素的宽度(包括内边距和边框)。
outerHeight() 方法返回元素的高度(包括内边距和边框)。
下面的例子返回指定的 <div> 元素的 outer-width/height:
$("button").click(function(){
  var txt="";
  txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
  txt+="Outer height: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});

outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距)。
outerHeight(true) 方法返回元素的高度(包括内边距、边框和外边距)。

jQuery - 更多的 width() 和 height()
下面的例子返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:
$("button").click(function(){
  var txt="";
  txt+="Document width/height: " + $(document).width();
  txt+="x" + $(document).height() + "\n";
  txt+="Window width/height: " + $(window).width();
  txt+="x" + $(window).height();
  alert(txt);
});

下面的例子设置指定的 <div> 元素的宽度和高度:
$("button").click(function(){
  $("#div1").width(500).height(500);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值