品牌显示隐藏练习
第一步编写页面内容
<!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><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></head><body><div name="code"><!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><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><!-- 引入jQuery --><script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script><script type="text/javascript">$(function(){ // 等待DOM加载完毕.var $category = $('ul li:gt(5):not(:last)'); // 获得索引值大于5的品牌集合对象(除最后一条)$category.hide(); // 隐藏上面获取到的jQuery对象。var $toggleBtn = $('div.showmore > a'); // 获取“显示全部品牌”按钮$toggleBtn.click(function(){ if($category.is(":visible")){$category.hide(); // 隐藏$category$('.showmore a span').css("background","url(img/down.gif) no-repeat 0 0") .text("显示全部品牌"); //改变背景图片和文本$('ul li').removeClass("promoted");// 去掉高亮样式 }else{$category.show(); // 显示$category$('.showmore a 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><div height="147" width="504" src="/img/2012/06/30/2007135367.png">图片2
?
?
?