JQUERY 控制内容的全部和部分显示

3 篇文章 0 订阅
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"content="text/html; 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 {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>

<script src="jquery.min.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
var $hiddenitems = $("ul li:gt(5):not(:last)");//定义索引值大于5且不包含最后一个li的对象为hiddenitems;
$hiddenitems.hide();  //$hiddenitems的对象隐藏

var $showsubmit = $("div.showmore>a");  //设置div中按钮的a为$showsubmit 
$showsubmit.click(function(){     //为$showsubmit定义点击事件方法
if($hiddenitems.is(":visible")){   //判断条件为设置默认隐藏的元素显示
$hiddenitems.hide();
$(".showmore a span") //设置点击按钮为对象
.css("background","url(img/down.gif) no-repeat 0 0")      //给选定对象添加CSS样式
.text("显示全部品牌");//给选定对象内的文字进行修改     
$('ul li').removeClass("promoted"); //移除样式


}
else{
$hiddenitems.show();
$(".showmore a span")
.css("background","url(img/up.gif) no-repeat 0 0")     
.text("精简显示品牌");                
$('ulli').filter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')")  //筛选含有佳能等文字内容的li
.addClass("promoted");

}
return false;
})
})
</script>
</head>

<body>
<divclass="SubCategoryBox">
<ul>
<li><ahref="#">佳能</a></li>
<li><ahref="#">索尼</a></li>
<li><ahref="#">三星</a></li>
<li><ahref="#">尼康</a></li>
<li><ahref="#">松下</a></li>
<li><ahref="#">卡西欧</a></li>
<li><ahref="#">富士</a></li>
<li><ahref="#">柯达</a></li>
<li><ahref="#">宾得</a></li>
<li><ahref="#">理光</a></li>
<li><ahref="#">奥林巴斯</a></li>
<li><ahref="#">明基</a></li>
<li><ahref="#">爱国者</a></li>
<li><ahref="#">其他品牌相机</a></li>
</ul>
<div class="showmore">
<a href="">
<span>显示所有品牌</span>
</a>
</div>

</div>
</body>
</html>

*所有确认DOM的括号内的内容都需要用引号括起来
JQ的赋值引用也是左右平等的,也就是$("xxx") =$("yyy"),因为是初学,右边多次少写了“$”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值