jqurey 学习笔记 传智博客佟老师附详细注释

1 、写 js 的时候用 aptana ( IDE ),有 jquery 的代码提示。

jqurey 学习笔记 传智博客佟老师附详细注释

 
把 code assist 里面的 jqurey1.3 选上就可以了。

2 、为一个元素增加事件的时候不要在 html 里加,在 js 中加。
不要写 <input type=”button” onclick=”xxx” id=”id”>
要写成: document.getElementById(“id”).onclick = function(){}

3 、把 js 剥离出来,做到 html 和 js 的分离。
Html 中除了引入 js 之外,不要别的 js 代码。

4 、 Jqurey 选择器:基本选择器、层级选择器、基本过滤选择器、内容过滤选择器、可见性过滤选择器、属性过滤选择器、子元素过滤选择器、表单选择器、表单对象属性选择器

5 、选择器中的空格问题

带空格的是子选择器,不带空格的是可见性过滤选择器

jqurey 学习笔记 传智博客佟老师附详细注释

例子 1 、品牌列表 需要一张小图片,还有 jqurey1.3.1 的 js 包
下载地址: jqurey鼠标经过例子
代码:

复制代码 代码如下:


<!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>
<script type="text/javascript" src="https://www.jb51.net/scripts/jquery-1.3.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){//dom 加载完毕时执行
// 得到 class 是 SubCategoryBox 的 div 中的 ul 中的 li ,索引大于 5 ,不选最后一个
// 即后两行
var $hideBandes = $("div.SubCategoryBox ul li:gt(5):not(:last)");
$hideBandes.hide();//dom 加载完毕先隐藏
// 给 class 为 showmore 的 div 中的所有 a 标记增加 click 方法
$("div.showmore>a").click(function(){
$hideBandes.toggle();// 点击是循环隐藏和显示
if($hideBandes.is(":visible")){// 如果是显示的
// 给几个特定的元素增加样式, div 中的 ul 中的 li 内容过滤 包含 佳能等几个内容时
$("div ul li").filter(":contains(' 佳能 '), :contains(' 尼康 '), :contains(' 奥林巴斯 ')")
.addClass("promoted");
$(".showmore>a>span").css("background", "url(img/up.gif) no-repeat 0 0");// 改变小箭头图片
$(".showmore>a>span").text(" 显示精简品牌 ");// 改变文字
}else{// 如果是隐藏的
// 去除加上去的样式
$("div ul li").filter(":contains(' 佳能 '), :contains(' 尼康 '), :contains(' 奥林巴斯 ')")
.removeClass("promoted");
$(".showmore>a>span").css("background", "url(img/down.gif) no-repeat 0 0");// 改变小箭头图片
$(".showmore>a>span").text(" 显示全部品牌 ^^");// 改变文字
}
return false;
});
});
</script>
</head>
<body>
<div>
<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="https://www.jb51.net/more.html"><span> 显示全部品牌 </span></a>
</div>
</div>
</body>
</html>


例子 2 、超链接和图片提示效果 需要几张图片 jqurey1.3.1 的 js 包
代码

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwwxx.html