jQuery 概述

仓库: 可以把很多东西放到这个仓库里面。找东西只需要到仓库里面查找到就可以了

JavaScript库: 即 library,是一个封装好的特定的集合(方法和函数)。从封装一大堆函数的角度理解库,就是在这个库中,封装了很多预先定义好的函数在里面,比如动画animate、hide、show,比如获取元素等.

常见的JavaScript库: jQuery,Prototype,YUI,Dojo,ExtJS,移动端的zepto这些库都是对原生JavaScript的封装,内部都是用JavaScript实现的

jQuery是一个快速、简洁的 JavaSript 库,其设计的宗旨是“write Less,Do More”,即导写更少的代码做更多的事情。

jQuery封装了 JavaScript 常用的功能代码,优化了 DOM 操作、事件处理、动画设计和Ajax交互学习
iQuery本质: 就是学习调用这些函数(方法)。
jQuery 出现的目的是加快前端人员的开发速度,我们可以非常方便的调用和使用它,从而提高开发效率。

  1. 轻量级。核心文件才几十kb,不会影响页面加载速度
  2. 跨浏览器兼容。基本兼容了现在主流的浏览器
  3. 链式程、隐式迭代
  4. 对事件、样式、动画支持,大大简化了DOM操作
  5. 支持插件扩展开发。有着丰富的第三方的插件,例如:
  6. 树形菜单、日期控件、轮播图等
  7. 免费、开源

jQuery基本使用-入口函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>

<body>
<script>
// $('div').hide();
// 1. 等着页面DOM加载完毕再去执行js 代码
// $(document).ready(function() {
// $('div').hide();
// })
// 2. 等着页面DOM加载完毕再去执行js 代码
$(function() {
$('div').hide();

})
</script>
<div></div>

</body>
</html>

jQuery顶级对象$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<body>
<div></div>
<script>
// 1. $ 是jQuery的别称(另外的名字)
// $(function() {
// alert(11)
// });
jQuery(function() {
// alert(11)
// $('div').hide();
jQuery('div').hide();
});
// 2. $同时也是jQuery的 顶级对象
</script>
</body>

DOM对象和jQuery对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<body>
<div></div>
<span></span>
<script>
// 1. DOM 对象: 用原生js获取过来的对象就是DOM对象
var myDiv = document.querySelector('div'); // myDiv 是DOM对象
var mySpan = document.querySelector('span'); // mySpan 是DOM对象
console.dir(myDiv);
// 2. jQuery对象: 用jquery方式获取过来的对象是jQuery对象。 本质:通过$把DOM元素进行了包装
$('div'); // $('div')是一个jQuery 对象
$('span'); // $('span')是一个jQuery 对象
console.dir($('div'));
// 3. jQuery 对象只能使用 jQuery 方法,DOM 对象则使用原生的 JavaScirpt 属性和方法
// myDiv.style.display = 'none';
// myDiv.hide(); myDiv是一个dom对象不能使用 jquery里面的hide方法
// $('div').style.display = 'none'; 这个$('div')是一个jQuery对象不能使用原生js 的属性和方法
</script>
</body>

DOM对象和jQuery对象相互转换

DOM对象与jQuery 对象之间是可以相互转换的
因为原生js比jQuery更大,原生的一些属性和方法jQuery没有给我们封装.要想使用这些属性和方法需要把jQuery对象转换为DOM对象才能使用。

  1. DOM对象转换为jQuery对象: $(DOM对象)

$(‘div’)

  1. jQuery对象转换为DOM对象(两种方式)

$('div')[index] index 是索引号
$(‘div’).get(index) index 是索引号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<body>
<video src="mov.mp4" muted></video>
<script>
// 1. DOM对象转换为 jQuery对象
// (1) 我们直接获取视频,得到就是jQuery对象
// $('video');
// (2) 我们已经使用原生js 获取过来 DOM对象
var myvideo = document.querySelector('video');
// $(myvideo).play(); jquery里面没有play 这个方法
// 2. jQuery对象转换为DOM对象
// myvideo.play();
$('video')[0].play()
$('video').get(0).play()
</script>
</body>

jQuery基本和层级选择器

$("选择器") // 里面选择器直接写CSS选择器即可,但是要加引号
名称 用法 描述
ID选择器 $(“#id”) 获取指定ID的元素
全选选择器 $(‘*’) 匹配所有元素
类选择器 $(“.class”) 获取同一类class的元素
标签选择器 $(“div”) 获取同一类标签的所有元素
并集选择器 $(“div,p,li”) 选取多个元素
交集选择器 $(“li.current”) 交集元素
名称 用法 描述
子代选择器 $(“ul>li”); 使用>号,获取亲儿子层级的元素;注意,并不会获取孙子层级的元素
后代选择器 $(“ul li”); 使用空格,代表后代选择器,获取ul下的所有li元素,包括孙子等

jQuery隐式迭代

jQuery设置样式

$('div').css('属性','值')

遍历内部DOM元素(伪数组形式存储)的过程就叫做隐式送代
简单理解:给匹配到的所有元素进行循环遍历,执行相应的方法,而不用我们再进行循环,简化我们的操作方便我们调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<body>
<div>惊喜不,意外不</div>
<div>惊喜不,意外不</div>
<div>惊喜不,意外不</div>
<div>惊喜不,意外不</div>
<ul>
<li>相同的操作</li>
<li>相同的操作</li>
<li>相同的操作</li>
</ul>
<script>
// 1. 获取四个div元素
console.log($("div"));

// 2. 给四个div设置背景颜色为粉色 jquery对象不能使用style
$("div").css("background", "pink");
// 3. 隐式迭代就是把匹配的所有元素内部进行遍历循环,给每一个元素添加css这个方法
$("ul li").css("color", "red");
</script>
</body>

jQuery筛选选择器

语法 用法 描述
:first $(‘li:first’) 获取第一个li元素
:last $(‘li:last’) 获取最后一个li元素
:eq(index) $(“li:eq(2)”) 获取到的li元素中,选择索引号为2的元素,索引号index从0开始。
:odd $(“li:odd”) 获取到的li元素中,选择索引号为奇数的元素
:even $(“li:even”) 获取到的li元素中,选择索引号为偶数的元素

jQuery筛选方法-选取父子元素

语法 用法 说明
parent() $(“li”).parent(); 查找父级
children(selector) $(“ul”).children(“li”) 相当于 $(“ul>li”),最近一级 (亲儿子)
find(selector) $(“ul”).find(“li”); 相当于$(“ul li”),后代选择器
siblings(selector) $(“.first”).siblings(“li”); 查找兄弟节点,不包括自己本身
nextAll([expr]) $(“.first”).nextAll() 查找当前元素之后所有的同辈元素
prevtA11([expr]) $(“.last”).prevAll() 查找当前元素之前所有的同辈元素
hasClass(class) $(“div”).hasClass(“protected”) 检查当前的元素是否含有某个特定的类,如果有,则返回true
eq(index) $(“li”).eq(2); 相当于$(“li:eq(2)”),index从0开始

新浪下拉菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}

li {
list-style-type: none;
}

a {
text-decoration: none;
font-size: 14px;
}

.nav {
margin: 100px;
}

.nav>li {
position: relative;
float: left;
width: 80px;
height: 41px;
text-align: center;
}

.nav li a {
display: block;
width: 100%;
height: 100%;
line-height: 41px;
color: #333;
}

.nav>li>a:hover {
background-color: #eee;
}

.nav ul {
display: none;
position: absolute;
top: 41px;
left: 0;
width: 100%;
border-left: 1px solid #FECC5B;
border-right: 1px solid #FECC5B;
}

.nav ul li {
border-bottom: 1px solid #FECC5B;
}

.nav ul li a:hover {
background-color: #FFF5DA;
}
</style>
<script src="jquery.min.js"></script>
</head>

<body>
<ul class="nav">
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="">私信</a>
</li>
<li>
<a href="">评论</a>
</li>
<li>
<a href="">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="">私信</a>
</li>
<li>
<a href="">评论</a>
</li>
<li>
<a href="">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="">私信</a>
</li>
<li>
<a href="">评论</a>
</li>
<li>
<a href="">@我</a>
</li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li>
<a href="">私信</a>
</li>
<li>
<a href="">评论</a>
</li>
<li>
<a href="">@我</a>
</li>
</ul>
</li>
</ul>
<script>
$(function() {
// 鼠标经过
$(".nav>li").mouseover(function() {
// $(this) jQuery 当前元素 this不要加引号
// show() 显示元素 hide() 隐藏元素
$(this).children("ul").show();
});
// 鼠标离开
$(".nav>li").mouseout(function() {
$(this).children("ul").hide();
})
})
</script>
</body>

</html>

jQuery排他思想

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="jquery.min.js"></script>
</head>

<body>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<script>
$(function() {
// 1. 隐式迭代 给所有的按钮都绑定了点击事件
$("button").click(function() {
// 2. 当前的元素变化背景颜色
$(this).css("background", "pink");
// 3. 其余的兄弟去掉背景颜色 隐式迭代
$(this).siblings("button").css("background", "");
});
})
</script>
</body>

</html>

淘宝服饰精品案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>

<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-size: 12px;
}

ul {
list-style: none;
}

a {
text-decoration: none;
}

.wrapper {
width: 250px;
height: 248px;
margin: 100px auto 0;
border: 1px solid pink;
border-right: 0;
overflow: hidden;
}

#left,
#content {
float: left;
}

#left li {
background: url(images/lili.jpg) repeat-x;
}

#left li a {
display: block;
width: 48px;
height: 27px;
border-bottom: 1px solid pink;
line-height: 27px;
text-align: center;
color: black;
}

#left li a:hover {
background-image: url(images/abg.gif);
}

#content {
border-left: 1px solid pink;
border-right: 1px solid pink;
}
</style>
<script src="jquery.min.js"></script>
<script>
$(function() {
// 1. 鼠标经过左侧的小li
$("#left li").mouseover(function() {
// 2. 得到当前小li 的索引号
var index = $(this).index();
console.log(index);
// 3. 让我们右侧的盒子相应索引号的图片显示出来就好了
// $("#content div").eq(index).show();
// 4. 让其余的图片(就是其他的兄弟)隐藏起来
// $("#content div").eq(index).siblings().hide();
// 链式编程
$("#content div").eq(index).show().siblings().hide();

})
})
</script>
</head>

<body>
<div class="wrapper">
<ul id="left">
<li><a href="#">女靴</a></li>
<li><a href="#">雪地靴</a></li>
<li><a href="#">冬裙</a></li>
<li><a href="#">呢大衣</a></li>
<li><a href="#">毛衣</a></li>
<li><a href="#">棉服</a></li>
<li><a href="#">女裤</a></li>
<li><a href="#">羽绒服</a></li>
<li><a href="#">牛仔裤</a></li>
</ul>
<div id="content">
<div>
<a href="#"><img src="images/女靴.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/雪地靴.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/冬裙.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/呢大衣.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/毛衣.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/棉服.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/女裤.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/羽绒服.jpg" width="200" height="250" /></a>
</div>
<div>
<a href="#"><img src="images/牛仔裤.jpg" width="200" height="250" /></a>
</div>

</div>


</div>
</body>

</html>

jQuery链式编程

链式编程是为了节省代码量,看起来更优雅

$(this).css('color, 'red').sibling().css('color','');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<body>
woshi body 的文字
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<script>
$(function() {
// 1. 隐式迭代 给所有的按钮都绑定了点击事件
$("button").click(function() {
// 2. 让当前元素颜色变为红色
// $(this).css("color", "red");
// 3. 让其余的姐妹元素不变色
// $(this).siblings().css("color", "");
// 链式编程
// $(this).css("color", "red").siblings().css("color", "");
// 我的颜色为红色, 我的兄弟的颜色为空
// $(this).siblings().css('color', 'red');
// 我的兄弟变为红色 ,我本身不变颜色
$(this).siblings().parent().css('color', 'blue');
// 最后是给我的兄弟的爸爸 body 变化颜色

});
})
</script>
</body>

jQuery修改样式css方法

Query可以使用css方法来修改简单元素样式;也可以操作类,修改多个样式

  1. 参数只写属性名,则是返回属性值

$(this).css(“color”);

  1. 参数是属性名,属性值,逗号分隔,是设置一组样式,属性必须加引号,值如果是数字可以不用跟单位和引号

$(this).css(“color”,”red”);

  1. 参数可以是对象形式,方便设置多组样式。属性名和属性值用冒号隔开,属性可以不用加引号

$(this).css((“color”:”white”,”font-size”:”20px”));

jQuery修改样式操作类

作用等同于以前的classList,可以操作类样式,注意操作类里面的参数不要加点

  1. 添加类

$(“div”).addClass(“current”);

  1. 移除类

$(“div”).removeClass(“current”);

  1. 切换

$(“div”).toggleClass(“current”);

tab栏切换案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}

li {
list-style-type: none;
}

.tab {
width: 978px;
margin: 100px auto;
}

.tab_list {
height: 39px;
border: 1px solid #ccc;
background-color: #f1f1f1;
}

.tab_list li {
float: left;
height: 39px;
line-height: 39px;
padding: 0 20px;
text-align: center;
cursor: pointer;
}

.tab_list .current {
background-color: #c81623;
color: #fff;
}

.item_info {
padding: 20px 0 0 20px;
}

.item {
display: none;
}
</style>
<script src="jquery.min.js"></script>
</head>

<body>
<div class="tab">
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>规格与包装</li>
<li>售后保障</li>
<li>商品评价(50000)</li>
<li>手机社区</li>
</ul>
</div>
<div class="tab_con">
<div class="item" style="display: block;">
商品介绍模块内容
</div>
<div class="item">
规格与包装模块内容
</div>
<div class="item">
售后保障模块内容
</div>
<div class="item">
商品评价(50000)模块内容
</div>
<div class="item">
手机社区模块内容
</div>

</div>
</div>
<script>
$(function() {
// 1.点击上部的li,当前li 添加current类,其余兄弟移除类
$(".tab_list li").click(function() {
// 链式编程操作
$(this).addClass("current").siblings().removeClass("current");
// 2.点击的同时,得到当前li 的索引号
var index = $(this).index();
console.log(index);
// 3.让下部里面相应索引号的item显示,其余的item隐藏
$(".tab_con .item").eq(index).show().siblings().hide();
});
})
</script>
</body>

</html>

jQuery类操作和className区别

原生JS中className会覆盖元素原先里面的类名
jQuery里面类操作只是对指定类进行操作,不影响原先的类名

jQuery显示与隐藏效果

显示隐藏效果

  1. 显示语法规范

show([speed,[easing],[fn]])

  1. 切换语法规范

toggle([speed,[easing],[fn]])

  1. 显示参数

(1) 参数都可以省略,无动画直接显示
(2) speed:三种预定速度之一的字符串(“slow”,”nomal”,or “fast”)或表示动画时长的毫秒数值(如:1000)。
(3) easing:(Optional)用来指定切换效果,默认是”swing”,可用参数”linear”
(4) fn:回调函数,在动画完成时执行的函数,每个元素执行一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<body>
<button>显示</button>
<button>隐藏</button>
<button>切换</button>
<div></div>
<script>
$(function() {
$("button").eq(0).click(function() {
$("div").show(1000, function() {
alert(1);
});
})
$("button").eq(1).click(function() {
$("div").hide(1000, function() {
alert(1);
});
})
$("button").eq(2).click(function() {
$("div").toggle(1000);
})
// 一般情况下,我们都不加参数直接显示隐藏就可以了
});
</script>
</body>

jQuery滑动效果以及事件切换

事件切换

hover([over,]out)

(1) over:鼠标移到元素上要触发的函数(相当于mouseenter)
(2) out:鼠标移出元素要触发的函数(相当于mouseleave )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<body>
<button>下拉滑动</button>
<button>上拉滑动</button>
<button>切换滑动</button>
<div></div>
<script>
$(function() {
$("button").eq(0).click(function() {
// 下滑动 slideDown()
$("div").slideDown();
})
$("button").eq(1).click(function() {
// 上滑动 slideUp()
$("div").slideUp(500);


})
$("button").eq(2).click(function() {
// 滑动切换 slideToggle()

$("div").slideToggle(500);

});

});
</script>
</body>

jQuery停止动画排队stop

  1. 动效果队列
    动画或者效果一旦触发就会执行,如果多次触发,就造成多个动画或者效果排队执行
  2. 停止排队stop()
    (1) stop()方法用于停止动画或效果
    (2) 注意: stop()写到动画或者效果的前面,相当于停止结束上一次的动画

jQuery淡入淡出以及突出显示案例

  1. 渐进方式调整到指定的不透明度

fadeTo([[speed],opacity,[easing],[fn]])

  1. 效果参数
    (1) opacity透明度必须写,取值 0~1之间
    (2) speed: 三种预定速度之一的字符(“sow”,”normal”or”fast”)或表示动画时长的毫秒数值(如:1000)。必须写
    (3) easing:(Optional)用来指定切换效果,默认是”swing”,可用参数”linear”
    (4) fn:回调函数,在动画完成时执行的函数,每个元素执行一次
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    div {
    width: 150px;
    height: 300px;
    background-color: pink;
    display: none;
    }
    </style>
    <script src="jquery.min.js"></script>
    </head>

    <body>
    <button>淡入效果</button>
    <button>淡出效果</button>
    <button>淡入淡出切换</button>
    <button>修改透明度</button>
    <div></div>
    <script>
    $(function() {
    $("button").eq(0).click(function() {
    // 淡入 fadeIn()
    $("div").fadeIn(1000);
    })
    $("button").eq(1).click(function() {
    // 淡出 fadeOut()
    $("div").fadeOut(1000);

    })
    $("button").eq(2).click(function() {
    // 淡入淡出切换 fadeToggle()
    $("div").fadeToggle(1000);

    });
    $("button").eq(3).click(function() {
    // 修改透明度 fadeTo() 这个速度和透明度要必须写
    $("div").fadeTo(1000, 0.5);

    });


    });
    </script>
    </body>

    </html>

jQuery自定义动画animate方法

  1. 语法

animate(params,[speed],[easing],[fn])

  1. 参数
    (1) params:想要更改的样式属性,以对象形式传递,必须写。属性名可以不用带引号,如果是复合属性则需要采取驼峰命名法borderLeft。其余参数都可以省略
    (2) speed:三种预定速度之一的字符审(“sow”,”normal”,or “fast”)或表示动画时长的毫秒数值(如:1000)
    (3) easing:(Optiona)用来指定切换效果,默认是”swing”,可用参数”linear”
    (4) fn回调函数,在动画完成时执行的函数,每个元素执行一次
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    <style>
    div {
    position: absolute;
    width: 200px;
    height: 200px;
    background-color: pink;
    }
    </style>
    </head>

    <body>
    <button>动起来</button>
    <div></div>
    <script>
    $(function() {
    $("button").click(function() {
    $("div").animate({
    left: 500,
    top: 300,
    opacity: .4,
    width: 500
    }, 500);
    })
    })
    </script>
    </body>

    </html>

王者荣耀手风琴案例制作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// 鼠标经过某个小li 有两步操作:
$(".king li").mouseenter(function() {
// 1.当前小li 宽度变为 224px, 同时里面的小图片淡出,大图片淡入
$(this).stop().animate({
width: 224
}).find(".small").stop().fadeOut().siblings(".big").stop().fadeIn();
// 2.其余兄弟小li宽度变为69px, 小图片淡入, 大图片淡出
$(this).siblings("li").stop().animate({
width: 69
}).find(".small").stop().fadeIn().siblings(".big").stop().fadeOut();
})



});
</script>

jQuery属性操作

设置或获取元素固有属性值 prop()
所谓元素固有属性就是元素本身自带的属性,比如<a>元素里面的 href,比如<input>元素里面的 type

  1. 获取属性语法prop(“属性)
  2. 设置属性语法prop(“属性”,”属性值”)

设置或获取元素自定义属性值 attr()
用户自己给元素添加的属性,我们称为自定义属性。比如给div 添加index = “1”

  1. 获取属性语法
    attr(“属性”) // 类似原生 getAttribute()
  2. 设置属性语法
    attr(“属性”,”属性值”) //类似原生 setAttribute()

数据缓存 data()
data() 方法可以在指定的元素上存取数据,并不会修改DOM元素结构。一旦页面刷新,之前存放的数据都将被移除。

  1. 附加数据语法
    data(“name”,value”) // 向被选元素附加数据
  2. 获取数据语法
    date(“name”)
    //向被选元素获取数据
    同时,还可以读取HTML5自定义属性 data-index,得到的是数字型
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    </head>

    <body>
    <a href="http://www.itcast.cn" title="都挺好">都挺好</a>
    <input type="checkbox" name="" id="" checked>
    <div index="1" data-index="2">我是div</div>
    <span>123</span>
    <script>
    $(function() {
    //1. element.prop("属性名") 获取元素固有的属性值
    console.log($("a").prop("href"));
    $("a").prop("title", "我们都挺好");
    $("input").change(function() {
    console.log($(this).prop("checked"));

    });
    // console.log($("div").prop("index"));
    // 2. 元素的自定义属性 我们通过 attr()
    console.log($("div").attr("index"));
    $("div").attr("index", 4);
    console.log($("div").attr("data-index"));
    // 3. 数据缓存 data() 这个里面的数据是存放在元素的内存里面
    $("span").data("uname", "andy");
    console.log($("span").data("uname"));
    // 这个方法获取data-index h5自定义属性 第一个 不用写data- 而且返回的是数字型
    console.log($("div").data("index"));

    })
    </script>
    </body>

    </html>

购物车模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$(function() {
// 1. 全选 全不选功能模块
// 就是把全选按钮(checkall)的状态赋值给 三个小的按钮(j-checkbox)就可以了
// 事件可以使用change
$(".checkall").change(function() {
// console.log($(this).prop("checked"));
$(".j-checkbox, .checkall").prop("checked", $(this).prop("checked"));
if ($(this).prop("checked")) {
// 让所有的商品添加 check-cart-item 类名
$(".cart-item").addClass("check-cart-item");
} else {
// check-cart-item 移除
$(".cart-item").removeClass("check-cart-item");
}
});
// 2. 如果小复选框被选中的个数等于3 就应该把全选按钮选上,否则全选按钮不选。
$(".j-checkbox").change(function() {
// if(被选中的小的复选框的个数 === 3) {
// 就要选中全选按钮
// } else {
// 不要选中全选按钮
// }
// console.log($(".j-checkbox:checked").length);
// $(".j-checkbox").length 这个是所有的小复选框的个数
if ($(".j-checkbox:checked").length === $(".j-checkbox").length) {
$(".checkall").prop("checked", true);
} else {
$(".checkall").prop("checked", false);
}
if ($(this).prop("checked")) {
// 让当前的商品添加 check-cart-item 类名
$(this).parents(".cart-item").addClass("check-cart-item");
} else {
// check-cart-item 移除
$(this).parents(".cart-item").removeClass("check-cart-item");
}
});
// 3. 增减商品数量模块 首先声明一个变量,当我们点击+号(increment),就让这个值++,然后赋值给文本框。
$(".increment").click(function() {
// 得到当前兄弟文本框的值
var n = $(this).siblings(".itxt").val();
// console.log(n);
n++;
$(this).siblings(".itxt").val(n);
// 3. 计算小计模块 根据文本框的值 乘以 当前商品的价格 就是 商品的小计
// 当前商品的价格 p
var p = $(this).parents(".p-num").siblings(".p-price").html();
// console.log(p);
p = p.substr(1);
console.log(p);
var price = (p * n).toFixed(2);
// 小计模块
// toFixed(2) 可以让我们保留2位小数
$(this).parents(".p-num").siblings(".p-sum").html("¥" + price);
getSum();
});
$(".decrement").click(function() {
// 得到当前兄弟文本框的值
var n = $(this).siblings(".itxt").val();
if (n == 1) {
return false;
}
// console.log(n);
n--;
$(this).siblings(".itxt").val(n);
// var p = $(this).parent().parent().siblings(".p-price").html();
// parents(".p-num") 返回指定的祖先元素
var p = $(this).parents(".p-num").siblings(".p-price").html();
// console.log(p);
p = p.substr(1);
console.log(p);
// 小计模块
$(this).parents(".p-num").siblings(".p-sum").html("¥" + (p * n).toFixed(2));
getSum();
});
// 4. 用户修改文本框的值 计算 小计模块
$(".itxt").change(function() {
// 先得到文本框的里面的值 乘以 当前商品的单价
var n = $(this).val();
// 当前商品的单价
var p = $(this).parents(".p-num").siblings(".p-price").html();
// console.log(p);
p = p.substr(1);
$(this).parents(".p-num").siblings(".p-sum").html("¥" + (p * n).toFixed(2));
getSum();
});
// 5. 计算总计和总额模块
getSum();

function getSum() {
var count = 0; // 计算总件数
var money = 0; // 计算总价钱
$(".itxt").each(function(i, ele) {
count += parseInt($(ele).val());
});
$(".amount-sum em").text(count);
$(".p-sum").each(function(i, ele) {
money += parseFloat($(ele).text().substr(1));
});
$(".price-sum em").text("¥" + money.toFixed(2));
}
// 6. 删除商品模块
// (1) 商品后面的删除按钮
$(".p-action a").click(function() {
// 删除的是当前的商品
$(this).parents(".cart-item").remove();
getSum();
});
// (2) 删除选中的商品
$(".remove-batch").click(function() {
// 删除的是小的复选框选中的商品
$(".j-checkbox:checked").parents(".cart-item").remove();
getSum();
});
// (3) 清空购物车 删除全部商品
$(".clear-all").click(function() {
$(".cart-item").remove();
getSum();
})
})

jQuery内容文本值

主要针对元素的内容还有表单的值操作。

  1. 普通元素内容html() (相当于原生innerHTML)
    html() // 获取元素的内容
    html(“内容”) //设置元素的内容

  2. 普通元素文本内容text() (相当与原生innerText)
    text() //获取元素的文本内容
    text(“文本内容”) //设置元素的文本内容

  3. 表单的值 val() (相当于原生value)

jQuery遍历对象each方法

主要是遍历、创建、添加、删除元素操作
遍历元素:jQuery隐式迭代是对同一类元素做了同样的操作。如果想要给同一类元素做不同操作,就需要用到遍历.
语法1:

$(“div”).each(function (index,domEle) { xxx; })

  1. each()方法遍历匹配的每一个元素。主要用DOM处理。each每一个
  2. 里面的回调函数有2个参数:index 是每个元素的索引号;demEle是每个DOM元素对象,不是jquery对象

jQuery遍历数据$.each

语法2:

$.each(object,function (index,element) { xxx;})

  1. $.each()方法可用于遍历任何对象。主要用于数据处理,比如数组,对象
  2. 里面的函数有2个参数:index是每个元素的索引号;element遍历内容

创建、添加、删除元素

创建元素
语法:$(<li> </li>");

  1. 内部添加 element.append(“内容”)
    把内容放入匹配元素内部最后面,类似原生appendChild。

  2. 外部添加
    element.after(“内容”) // 把内容放入目标元秦后面
    element.before(“内容”) // 把内容放入目标元素前面
    ①内部添加元素,生成之后,它们是父子关系
    ②外部添加元素,生成之后,他们是兄弟关系.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    </head>

    <body>
    <ul>
    <li>原先的li</li>
    </ul>
    <div class="test">我是原先的div</div>
    <script>
    $(function() {
    // 1. 创建元素
    var li = $("<li>我是后来创建的li</li>");
    // 2. 添加元素

    // (1) 内部添加
    // $("ul").append(li); 内部添加并且放到内容的最后面
    $("ul").prepend(li); // 内部添加并且放到内容的最前面

    // (2) 外部添加
    var div = $("<div>我是后妈生的</div>");
    // $(".test").after(div);
    $(".test").before(div);
    // 3. 删除元素
    // $("ul").remove(); 可以删除匹配的元素 自杀
    // $("ul").empty(); // 可以删除匹配的元素里面的子节点 孩子
    $("ul").html(""); // 可以删除匹配的元素里面的子节点 孩子

    })
    </script>
    </body>

    </html>

jQuery尺寸方法

语法 用法
width() / height() 取得匹配元素宽度和高度值 只算 width / height
innerWidth() / innerHieght() 取得匹配元素宽度和高度值 包含 padding
outerWidth() / outerHeight() 取得匹配元素宽度和高度值 包含 padding 、border
outerWidth(true) / outerHeight(true) 取得匹配元素宽度和高度值 包含 padding、borde、margin

以上参数为空,则是获取相应值,返回的是数字型
如果参数为数字,则是修改相应值。参数可以不必写单位

jQuery位置方法

  1. offset()设置或获取元素偏移
    ① offset()方法设置或返回被选元素相对于文档的偏移坐标,跟父级没有关系
    ② 该方法有2个属性left、top。offset().top 用于获取距离文档顶部的距离,offse().left 用于获取距离文档左侧的距离.
    ③ 可以设置元素的偏移: offset({top: 10, left: 30});

  2. position()获取元素偏移
    position()方法用于返回被选元素相对于带有定位的父级偏移坐标,如果父级都没有定位,则以文档为准

jQuery被卷去头部方法

  1. scrollTop()/scrollLeft() 设置或获取元素被卷去的头部和左侧
    ① scrolITop() 方法设置或返回被选元素被卷去的头部

电梯导航案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$(function() {
// 当我们点击了小li 此时不需要执行 页面滚动事件里面的 li 的背景选择 添加 current
// 节流阀 互斥锁
var flag = true;
// 1.显示隐藏电梯导航
var toolTop = $(".recommend").offset().top;
toggleTool();

function toggleTool() {
if ($(document).scrollTop() >= toolTop) {
$(".fixedtool").fadeIn();
} else {
$(".fixedtool").fadeOut();
};
}

$(window).scroll(function() {
toggleTool();
// 3. 页面滚动到某个内容区域,左侧电梯导航小li相应添加和删除current类名


if (flag) {
$(".floor .w").each(function(i, ele) {
if ($(document).scrollTop() >= $(ele).offset().top) {
console.log(i);
$(".fixedtool li").eq(i).addClass("current").siblings().removeClass();

}
})
}



});
// 2. 点击电梯导航页面可以滚动到相应内容区域
$(".fixedtool li").click(function() {
flag = false;
console.log($(this).index());
// 当我们每次点击小li 就需要计算出页面要去往的位置
// 选出对应索引号的内容区的盒子 计算它的.offset().top
var current = $(".floor .w").eq($(this).index()).offset().top;
// 页面动画滚动效果
$("body, html").stop().animate({
scrollTop: current
}, function() {
flag = true;
});
// 点击之后,让当前的小li 添加current 类名 ,姐妹移除current类名
$(this).addClass("current").siblings().removeClass();
})
})

事件处理on绑定一个或者多个事件

on()方法在匹配元素上绑定一个或多个事件的事件处理函数
语法:

element.on (events,[selector],fn)

  1. events:一个或多个用空格分隔的事件类型,如”click”或”keydown”
  2. selector:元素的子元素选择器
  3. fn:回调函数 即绑定在元素身上的侦听函数

on实现事件委派和给动态元素绑定事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
}

.current {
background-color: purple;
}
</style>
<script src="jquery.min.js"></script>
</head>

<body>
<div></div>
<ul>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
</ul>
<ol>

</ol>
<script>
$(function() {
// 1. 单个事件注册
// $("div").click(function() {
// $(this).css("background", "purple");
// });
// $("div").mouseenter(function() {
// $(this).css("background", "skyblue");
// });

// 2. 事件处理on
// (1) on可以绑定1个或者多个事件处理程序
// $("div").on({
// mouseenter: function() {
// $(this).css("background", "skyblue");
// },
// click: function() {
// $(this).css("background", "purple");
// },
// mouseleave: function() {
// $(this).css("background", "blue");
// }
// });
$("div").on("mouseenter mouseleave", function() {
$(this).toggleClass("current");
});
// (2) on可以实现事件委托(委派)
// $("ul li").click();
$("ul").on("click", "li", function() {
alert(11);
});
// click 是绑定在ul 身上的,但是 触发的对象是 ul 里面的小li
// (3) on可以给未来动态创建的元素绑定事件
// $("ol li").click(function() {
// alert(11);
// })
$("ol").on("click", "li", function() {
alert(11);
})
var li = $("<li>我是后来创建的</li>");
$("ol").append(li);
})
</script>
</body>

</html>

微博发布案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>

<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0
}

ul {
list-style: none
}

.box {
width: 600px;
margin: 100px auto;
border: 1px solid #000;
padding: 20px;
}

textarea {
width: 450px;
height: 160px;
outline: none;
resize: none;
}

ul {
width: 450px;
padding-left: 80px;
}

ul li {
line-height: 25px;
border-bottom: 1px dashed #cccccc;
display: none;
}

input {
float: right;
}

ul li a {
float: right;
}
</style>
<script src="jquery.min.js"></script>
<script>
$(function() {
// 1.点击发布按钮, 动态创建一个小li,放入文本框的内容和删除按钮, 并且添加到ul 中
$(".btn").on("click", function() {
var li = $("<li></li>");
li.html($(".txt").val() + "<a href='javascript:;'> 删除</a>");
$("ul").prepend(li);
li.slideDown();
$(".txt").val("");
})

// 2.点击的删除按钮,可以删除当前的微博留言li
// $("ul a").click(function() { // 此时的click不能给动态创建的a添加事件
// alert(11);
// })
// on可以给动态创建的元素绑定事件
$("ul").on("click", "a", function() {
$(this).parent().slideUp(function() {
$(this).remove();
});
})

})
</script>
</head>

<body>
<div class="box" id="weibo">
<span>微博发布</span>
<textarea name="" class="txt" cols="30" rows="10"></textarea>
<button class="btn">发布</button>
<ul>
</ul>
</div>
</body>

</html>

off解绑事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
}
</style>
<script src="jquery.min.js"></script>
<script>
$(function() {
$("div").on({
click: function() {
console.log("我点击了");
},
mouseover: function() {
console.log('我鼠标经过了');
}
});
$("ul").on("click", "li", function() {
alert(11);
});
// 1. 事件解绑 off
// $("div").off(); // 这个是解除了div身上的所有事件
$("div").off("click"); // 这个是解除了div身上的点击事件
$("ul").off("click", "li");
// 2. one() 但是它只能触发事件一次
$("p").one("click", function() {
alert(11);
})
})
</script>
</head>

<body>
<div></div>
<ul>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
<li>我们都是好孩子</li>
</ul>
<p>我是屁</p>
</body>

</html>

jQuery自动触发事件

element.click() // 第一种简写形式
element.trigger(“type”) // 第二种自动触发模式
element.triggerHandler(type) // 第三种自动触发模式

jQuery其他方法导读

element.on (events,[selector],function(event) {})
阻止默认行为:event.preventDefault() 或者return false
阻止冒泡:event.stopPropagation()

jQuery对象拷贝extend (选放)

语法:

$.extend([deep], target, object1, [objectN])

  1. deep:如果设为true为深拷贝,默认为false 浅拷贝
  2. target:要拷贝的目标对象
  3. object1:待拷贝到第一个对象的对象
  4. objectN:待拷贝到第N个对象的对象
  5. 浅拷贝是把被拷贝的对象复杂数据类型中的地址拷贝给目标对象,修改目标对象会影响被拷贝对象。
  6. 深拷贝,前面加true,完全克隆(拷贝的对象,而不是地址),修改目标对象不会影响被拷贝对象

jQuery多库共存

问题概述:
jQuery使用$作为标示符,随着jQuery的流行其他js库也会用这作为标识符,这样一起使用会引起冲突
客观需求:
需要一个解决方案,让jQuery和其他的js库不存在冲突,可以同时存在,这就叫做多库共存。
jQuery解决方案:

  1. 把里面的 $ 符号统一改为jQuery。比如jQuery(“div”)
  2. jQuery变量规定新的名称: $.noConflict() var xx = $.noConflict();
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    <script>
    $(function() {
    function $(ele) {
    return document.querySelector(ele);
    }
    console.log($("div"));
    // 1. 如果$ 符号冲突 我们就使用 jQuery
    jQuery.each();
    // 2. 让jquery 释放对$ 控制权 让用自己决定
    var suibian = jQuery.noConflict();
    console.log(suibian("span"));
    suibian.each();
    })
    </script>
    </head>

    <body>
    <div></div>
    <span></span>
    </body>

    </html>

瀑布流插件使用

jQuery功能比较有限,想要更复杂的特效效果,可以借助于jQuery插件完成
注意:这些插件也是依赖于jQuery来完成的,所以必须要先引入jQuery文件,因此也称为jQuery插件.
jQuery插件常用的网站:

  1. jQuery插件库 http://www.jq22.com/
  2. jQuery之家 http://www.htmleaf.com/

jQuery插件使用步骤:

  1. 引入相关文件。(jQuery文件和插件文件)
  2. 复制相关html、css、js(调用插件)。

图片懒加载技术

jQuery插件演示:
图片懒加载(图片使用延迟加载在可提高网页下载速度。它也能帮助减轻服务器负载)
当我们页面滑动到可视区域,再显示图片。
我们使用jquery插件库EasyLazyload。注意,此时js引入文件和js调用必须写到DOM元素(图片)最后面

全屏滚动插件使用(选放)

全屏滚动(fullpage.js)
gitHub : https://github.com/alvarotrigo/fullPage.js
中文翻译网站:http://www.dowebok.com/demo/2014/77/