显示隐藏文本

在学锋利的jQuery,就把例子一个个写了下来。

这是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 class="showmore">
<a href="#"><span>显示全部品牌</span></a>
</div>
</div>

这是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 {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;}

这是js:

$(function(){
//$('ul li:gt(5):not(:last)');先获取ul元素下索引值大于5的<li>元素的集合元素,然后去掉集合中的最后一个元素。
//:not()选择器,选取除了指定元素以外的所有元素。
var $category = $('ul li:gt(5):not(:last)');
$category.hide();
var $toggleBtn = $('div .showmore>a');
$toggleBtn.click(function(){
if($category.is(':visible')){
$category.hide();
//text(string),设置所有匹配元素的文本内容
$(this).find('span').css('background','url(img/down.gif) no-repeat 0 0').text('显示全部品牌');
$('ul li').removeClass('promoted');
} else {
$category.show();
$(this).find('span').css('background','url(img/up.gif) no-repeat 0 0').text('精简显示品牌');
//filter(expr),筛选出与指定表达式匹配的元素集合,其中expr可以是多个选择器的组合。
//注意区分和find()的方法,find()会在元素内寻找寻找匹配元素,是对它的子集操作。而filter()则是筛选元素,是对自身集合元素进行筛选。
//:contains(),选择器选取包含指定字符串的元素
$('ul li').filter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')").addClass('promoted');
}
});
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值