(转)jquery基础教程四(css的操作之css,height和width)

上面我们学习了jquery 的 addClass和removeClass方法

下面我们来看看 css,height和width
首先我们看看 css(key, value)css(name)

css(key, value)

在所有匹配的元素中,设置一个样式属性的值。

css(name)

访问第一个匹配元素的样式属性。使用这个方法可以很容易地取得第一个匹配元素的样式属性的值。

css(properties)

把一个“名/值对”对象设置为所有匹配元素的样式属性。 这是一种在所有匹配的元素上设置大量样式属性的最佳方式。

首先我们看看

css(key, value)

我们看看下面代码

$("#css1").click(function(){
       $(this).css("border","2px dashed #000000");
    })

当鼠标单击ID等于css1的元素 此元素的border属性设置成 "2px dashed #000000"

css(name)

$("#css2").click(function(){
       $(this).css("border","2px dashed #6600FF");
       alert($(this).css("border"));
    })

$(this).css("border","2px dashed #6600FF");

当鼠标单击ID等于css2的元素 此元素的border属性设置成 "2px dashed #6600FF"

alert($(this).css("border"));

获得border的属性值.

css(properties)

$("#css3").click(function(){
       $(this).css({ color: "red", background: "blue" });
    })

当鼠标单击ID等于css3的元素 样式color为red background为blue


下面我们看看height和width用法 其实从单词意思上也应该知道的差不多

height(val)

为每个匹配的元素设置CSS高度(height)属性的值。如果没有明确指定单位(如:em或%),使用px。

height()

取得第一个匹配元素当前计算的高度值(px)。

height(val)

$("#ht1").click(function(){
       $(this).height("200px");
    alert($(this).height());
    })
$(this).height("200px"); 当鼠标单击ID等于ht1的元素 此元素的高度将变成200px

alert($(this).height());显示height的值

width(val) width() 和height是一样的 只不过就是设置宽度.这里就不多说了.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery基础教程四(css的操作之css,height和width)</title>
<script language="javascript" src="     $("#css1").click(function(){
       $(this).css("border","2px dashed #000000");
    })
    $("#css2").click(function(){
       $(this).css("border","2px dashed #6600FF");
       alert($(this).css("border"));
    })
    $("#css3").click(function(){
       $(this).css({ color: "red", background: "blue" });
    })
     $("#ht1").click(function(){
       $(this).height("200px");
    alert($(this).height());
    })
})
//--><br%20/>%0D%0A</SCRIPT><br%20/>%0D%0A</head><br%20/>%0D%0A<body><br%20/>%0D%0Acss(key,%20value)%20%E5%9C%A8%E6%89%80%E6%9C%89%E5%8C%B9%E9%85%8D%E7%9A%84%E5%85%83%E7%B4%A0%E4%B8%AD%EF%BC%8C%E8%AE%BE%E7%BD%AE%E4%B8%80%E4%B8%AA%E6%A0%B7%E5%BC%8F%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC%E3%80%82%20<br%20/>%0D%0A<div%20ID=css1>jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth)%20%E5%BD%93%E9%BC%A0%E6%A0%87%E7%82%B9%E5%87%BB%20%E6%89%A7%E8%A1%8C%20%24(this).css(%22border%22,%222px%20dashed%20#000000%22);</div><br><br><br%20/>%0D%0Acss(name)%20%E8%AE%BF%E9%97%AE%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8C%B9%E9%85%8D%E5%85%83%E7%B4%A0%E7%9A%84%E6%A0%B7%E5%BC%8F%E5%B1%9E%E6%80%A7%E3%80%82%E4%BD%BF%E7%94%A8%E8%BF%99%E4%B8%AA%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E5%BE%88%E5%AE%B9%E6%98%93%E5%9C%B0%E5%8F%96%E5%BE%97%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8C%B9%E9%85%8D%E5%85%83%E7%B4%A0%E7%9A%84%E6%A0%B7%E5%BC%8F%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC%E3%80%82</span></span></p>%0D%0A<p><span%20class=%22fn1%22><span%20class=%22fn1%22><div%20ID=css2>jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth)%20%E5%BD%93%E9%BC%A0%E6%A0%87%E7%82%B9%E5%87%BB%20%E6%89%A7%E8%A1%8C%20%24(this).css(%22border%22,%2210px%20dashed%20#6600FF%22);%20alert(%24(this).css(%22border%22));</div><br><br><br%20/>%0D%0Acss(properties)%E6%8A%8A%E4%B8%80%E4%B8%AA%E2%80%9C%E5%90%8D/%E5%80%BC%E5%AF%B9%E2%80%9D%E5%AF%B9%E8%B1%A1%E8%AE%BE%E7%BD%AE%E4%B8%BA%E6%89%80%E6%9C%89%E5%8C%B9%E9%85%8D%E5%85%83%E7%B4%A0%E7%9A%84%E6%A0%B7%E5%BC%8F%E5%B1%9E%E6%80%A7%E3%80%82%20%E8%BF%99%E6%98%AF%E4%B8%80%E7%A7%8D%E5%9C%A8%E6%89%80%E6%9C%89%E5%8C%B9%E9%85%8D%E7%9A%84%E5%85%83%E7%B4%A0%E4%B8%8A%E8%AE%BE%E7%BD%AE%E5%A4%A7%E9%87%8F%E6%A0%B7%E5%BC%8F%E5%B1%9E%E6%80%A7%E7%9A%84%E6%9C%80%E4%BD%B3%E6%96%B9%E5%BC%8F</span></span></p>%0D%0A<p><span%20class=%22fn1%22><span%20class=%22fn1%22><div%20ID=css3>jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth)%20%E5%BD%93%E9%BC%A0%E6%A0%87%E7%82%B9%E5%87%BB%20%E6%89%A7%E8%A1%8C%20%24(this).css(%7B%20color:%20%22red%22,%20background:%20%22blue%22%20%7D);%20</div><br><br></span></span></p>%0D%0A<p><span%20class=%22fn1%22><span%20class=%22fn1%22>height(val)%E4%B8%BA%E6%AF%8F%E4%B8%AA%E5%8C%B9%E9%85%8D%E7%9A%84%E5%85%83%E7%B4%A0%E8%AE%BE%E7%BD%AECSS%E9%AB%98%E5%BA%A6(height)%E5%B1%9E%E6%80%A7%E7%9A%84%E5%80%BC%E3%80%82%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%9C%89%E6%98%8E%E7%A1%AE%E6%8C%87%E5%AE%9A%E5%8D%95%E4%BD%8D%EF%BC%88%E5%A6%82%EF%BC%9Aem%E6%88%96%%EF%BC%89%EF%BC%8C%E4%BD%BF%E7%94%A8px%E3%80%82</span></span></p>%0D%0A<p><span%20class=%22fn1%22><span%20class=%22fn1%22><br%20/>%0D%0A<div%20ID=ht1>jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth)%20%E5%BD%93%E9%BC%A0%E6%A0%87%E7%82%B9%E5%87%BB%20%E6%89%A7%E8%A1%8C%20%24(this).height(%22200px%22);alert(%24(this).height());%20</div><br><br><br%20/>%0D%0A</body><br%20/>%0D%0A</html><br%20/>%0D%0A<br%20/>%0D%0A</span></span></p>%0D%0A<p>%E7%A8%8B%E5%BA%8F%E6%BC%94%E7%A4%BA%E5%9C%B0%E5%9D%80:<a%20href=%22http://www.cnjquery.com/demo/jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth).html%22%20target=%22_blank%22>http://www.cnjquery.com/demo/jquery%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E5%9B%9B(css%E7%9A%84%E6%93%8D%E4%BD%9C%E4%B9%8Bcss,height%E5%92%8Cwidth).html</a></p>%0D%0A</div><div%20id=%22MySignature%22></div>%0D%0A<div%20class=%22clear%22></div>%0D%0A<div%20id=%22blog_post_info_block%22>%0D%0A<div%20id=%22BlogPostCategory%22></div>%0D%0A<div%20id=%22EntryTag%22></div>%0D%0A<div%20id=%22blog_post_info%22>%0D%0A</div>%0D%0A<div%20class=%22clear%22></div>%0D%0A<div%20id=%22post_next_prev%22></div>%0D%0A</div>%0D%0A%0D%0A%0D%0A%09%09</div>%0D%0A%09%09<div%20class%20=%20%22postDesc%22>posted%20@%20<span%20id=%22post-date%22>2008-12-20%2019:55</span>%20<a%20href=" https:>mysun 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mzoylee/archive/2008/12/20/1359015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值