注意:
1.show(); 显示隐藏的匹配元素
2.css(name, value); 给元素设置样式
3.text(string); 设置匹配元素的文本内容
4.filter(expr); 筛选出与制定表达式匹配的元素集合,可以是多个选择器的组合。注意和find()的区别。find()会在元素内寻找匹配元素,而filter()是筛选元素。一个是对它的子集操作,一个是对自身集合元素进行筛选。
5.addClass(class) 为匹配元素添加制定类名 去除class使用removeClass()
6.toggle()方法可以取代 :visible判断,后面会说
7.return false 告诉浏览器不要跳转链接
完整代码:
<!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></title>
<link rel="stylesheet" type="text/css" href="css/default.css" />
<script src="../../scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(function()
{
var $category = $('ul li:gt(5):not(:last)');
$category.hide();
var $toggleBtn = $('div.showmore > a');
$toggleBtn.click(function()
{
if ($category.is(":visible"))
{
$category.hide();
$(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("精简显示品牌");
$('ul li').filter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')").addClass("promoted");
}
return false;
})
})
</script>
</head>
<body>
<p>精简:</p>
<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 style="display:none"><a href="#">富士</a><i>(14894) </i></li>
<li style="display:none"><a href="#">柯达</a><i>(9520) </i></li>
<li style="display:none"><a href="#">宾得</a><i>(2195) </i></li>
<li style="display:none"><a href="#">理光</a><i>(4114) </i></li>
<li style="display:none"><a href="#">奥林巴斯</a><i>(12205) </i></li>
<li style="display:none"><a href="#">明基</a><i>(1466) </i></li>
<li style="display:none"><a href="#">爱国者</a><i>(3091) </i></li>
<li ><a href="#">其它品牌相机</a><i>(7275) </i></li>
</ul>
<div class="showmore">
<a href="more.html"><span>显示全部品牌</span></a>
</div>
</div>
</body>
</html>