jquery—class样式改变

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{ margin:0; padding:0;}
body {font-size:12px;text-align:center;}
a { color:#04D; text-decoration:none;}
a:hover { color:#F50; text-decoration:underline;}
.SubCategoryBox {border: 1px red solid; width:600px; margin:0 auto; text-align:center;margin-top:40px;}
.SubCategoryBox ul { list-style:none;}
.SubCategoryBox ul li { display:block; float:left; width:200px; line-height:20px;}
.showmore { clear:both; text-align:center;padding-top:10px;}
.showmore a { display:block; width:120px; margin:0 auto; line-height:24px; border:1px solid #AAA;}

.showmore a span { padding-left:15px; background:url(img/down.gif) no-repeat 0 0;}

.promoted a { color:#F50;}
</style>

<!--jquery-->
<script type="text/javascript" src="jquery-1.7.2.js" ></script>
<script type="text/javascript">

$(function(){
//1. hasClass 查看该div是否存在样式,有返回true,没有返回false
alert($("div:first").hasClass("SubCategoryBox"));


$(".showmore0").click(function(){
//2.removeClass 移除样式
alert("样式移除");
$("div:first").removeClass("SubCategoryBox");
})

$(".showmore").click(function(){
//3.添加样式,将样式返回
alert("样式添加回去");
$("div:first").addClass("SubCategoryBox");
})

$(".showmore1").click(function(){
//4.切换样式,toggleClass 若样式存在则去除,若不在则添加回去
alert("样式切换");
$("div:first").toggleClass("SubCategoryBox");
})


$(".showmore2").click(function(){
//5.CSS改变透明度
alert("透明度改变");
$("div:first").css("opacity","0.2");
})


$(".showmore3").click(function(){
//6.改变宽度
alert("当前宽度为"+$("div:first").width());
alert("当前高度为"+$("div:first").height());
$("div:first").width(100);
$("div:first").height(100);
})

$(".showmore4").click(function(){
//7. 获取元素在当前视窗中的相对位移: offset(). 
//其返回对象包含了两个属性: top, left. 该方法只对可见元素有效
alert("当前top:"+$("div:first").offset().top);
alert("当前left:"+$("div:first").offset().left);
})


})

</script>
</head>
<body>
<div class="SubCategoryBox">
<ul>
<li ><a href="#">佳能</a><i>(30440) </i></li>
<li ><a href="#">索尼</a><i>(27220) </i></li>
<li ><a href="#">三星</a><i>(20808) </i></li>
<li ><a href="#">尼康</a><i>(17821) </i></li>
<li ><a href="#">松下</a><i>(12289) </i></li>
<li ><a href="#">卡西欧</a><i>(8242) </i></li>
<li ><a href="#">富士</a><i>(14894) </i></li>
<li ><a href="#">柯达</a><i>(9520) </i></li>
<li ><a href="#">宾得</a><i>(2195) </i></li>
<li ><a href="#">理光</a><i>(4114) </i></li>
<li ><a href="#">奥林巴斯</a><i>(12205) </i></li>
<li ><a href="#">明基</a><i>(1466) </i></li>
<li ><a href="#">爱国者</a><i>(3091) </i></li>
<li ><a href="#">其它品牌相机</a><i>(7275) </i></li>
</ul>

<div>

<a href="#" class="showmore0"><span>样式移除</span></a>
<a href="#" class="showmore"><span>添加样式</span></a>
<a href="#" class="showmore1"><span>切换样式</span></a>
<a href="#" class="showmore2"><span>改变透明度</span></a>
<a href="#" class="showmore3"><span>改变宽高</span></a>
<a href="#" class="showmore4"><span>获取相对位移</span></a>
</div>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值