WEB前端--JQuery

jQuery

用法:http://www.php100.com/manual/jquery/

教程:http://www.w3school.com.cn/jquery/index.asp


一、jQuery基础

1、创建jQuery对象

  • $  ==>  相当于创建一个jQuery对象-->$=jQuery()

  • $('#id')  ==>  相当于document.getElementById('id'),找到id是‘id’的元素。

  • $() ==> 主体框架加载完成

  • $(document).ready(function(){}) ==> 页面文档加载完成后执行函数

2、当页面框架加载完成,执行这里面的代码.

1
2
3
4
5
6
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(function(){
        alert('当页面框架加载完成,执行这里面的代码.');
    });
</script>

3、把id=i1的元素值改成456

1
2
3
4
5
6
7
<div id="i1">123</div>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(function(){
        $('#i1').text('456');
    });
</script>

4、增加样式

1
2
3
4
5
6
7
8
9
10
11
12
13
    <style>
        .red{
            background-color: red;
        }
    </style>
 
<div id="i1" class="c1">123</div>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(function(){
        $('.c1').addClass('red')
    });
</script>

5、map​

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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <table>
        <thead></thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td onclick="get_prev(this)">编辑</td>
            </tr>
        </tbody>
    </table>
    <script type="text/javascript" src="jquery-2.1.4.min.js"></script>
    <script type="text/javascript">
        function get_prev(arg){
            //$(arg).siblings(),所有兄弟标签
            //循环多个标签中的每一个标签
            //每一个标签被循环时,都会执行map内部的函数并获取其返回值
            //将所有的返回值封装到一个数组(列表)中
            //返回列表
            var list=$(arg).siblings().map(function(){
                //当前循环的标签
                return $(this).text();
            });
            console.log(list[0],list[1],list[2]);
        }
    </script>
</body>
</html>


更多用法:http://www.php100.com/manual/jquery/

二、jQuery使用

1、this和$(this)区别

  • this是HTML的一个元素(id为textbox),直接改元素的属性:this.title="ok"

  • $(this)是一个jQuery对象,后面要跟jQuery属性,如:$(this).attr('title','ok')

this举例:
1
2
3
4
5
6
7
8
$("#textbox").hover(
    function() {  
        this.title = "Test";
    },  
    fucntion() {  
        this.title = "OK”;  
    }
);
$(this)举例:
1
2
3
4
5
6
7
8
$("#textbox").hover(
    function() {
        $(this).attr("title","Test");
    },
    fucntion() {
        $(this).attr("title","ok");
    }
);


三、实例

1、返回顶部

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">
    <title></title>
    <style>
        .back{
            position: fixed;
            bottom: 0px;
            right: 0px;
        }
        .hide{
            display: none;
        }
    </style>
</head>
<body>
 
<div style="height: 2000px;"></div>
 
<div onclick="GoTop()" class="back hide">返回顶部</div>
 
<script src="jquery-2.1.4.js"></script>
<script type="text/javascript">
 
    function GoTop(){
        //返回顶部
        $(window).scrollTop(0);
    }
 
    $(function(){
 
        $(window).scroll(function(){
            //当滚动滑轮时,执行函数体
 
            //获取当前滑轮滚动的高度
            var top = $(window).scrollTop();
 
            if(top>100){
                //展示“返回顶部”
                $('.back').removeClass('hide');
            }else{
                //隐藏“返回顶部”
                $('.back').addClass('hide');
            }
        });
    });
 
</script>
 
</body>
</html>

2、多选框

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>
    <head>
        <meta charset='utf-8' />
        <title></title>
        <script src='jquery-2.1.4.js'></script>
        <script type="text/javascript">
            $(function(){
                $('#selectAll').click(function(){
                    $('#checklist :checkbox').attr('checked',true);
                });
                $('#unselectAll').click(function(){
                    $('#checklist :checkbox').attr('checked',false);
                });
                $('#reverseAll').click(function(){
                    $('#checklist :checkbox').each(function(){
                        $(this).attr('checked',!$(this).attr('checked'))
                    })
                })
 
            })           
        </script>
    </head>
    <body>
        <div id='checklist'>
            <input type='checkbox' value='1'/>篮球
            <input type='checkbox' value='2'/>足球
            <input type='checkbox' value='3'/>羽毛球
        </div>
        <input type='button' value='全选' id='selectAll' />
        <input type='button' value='不选' id='unselectAll' />
        <input type='button' value='反选' id='reverseAll' />
    </body>
</html>

暂时不太好使

3、菜单

列出详细菜单列表,其它的收回。

css:
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
.hide{
    displaynone;
}
 
.container{
    width:300px;
    height600px;
    background-color#ddd;
    border1px solid #999;
}
 
.container .title{
    height38px;
    font-size28px;
    line-height38px;
    background-color: orange;
    cursorpointer;
}
 
.container .body{
    background-color:white;
}
 
.container .body a{
    display:block;
    padding10px;
}
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
<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8' />
        <link rel="stylesheet" type="text/css" href="common.css" />
        <script type="text/javascript" src='jquery-2.1.4.js'></script>
 
    </head>
    <body>
        <div class='container'>
            <div>
                <div class='title'>Menu1</div>
                <div class='body'>
                    <a href="">content1</a>
                    <a href="">content2</a>
                    <a href="">content3</a>
                </div>
            </div>
 
            <div>
                <div class='title'>Menu1</div>
                <div class='body hide'>
                    <a href="">content1</a>
                    <a href="">content2</a>
                    <a href="">content3</a>
                </div>
            </div>
 
            <div>
                <div class='title'>Menu1</div>
                <div class='body hide'>
                    <a href="">content1</a>
                    <a href="">content2</a>
                    <a href="">content3</a>
                </div>
            </div>
             
            <div>
                <div class='title'>Menu1</div>
                <div class='body hide'>
                    <a href="">content1</a>
                    <a href="">content2</a>
                    <a href="">content3</a>
                </div>
            </div>
             
            <div>
                <div class='title'>Menu1</div>
                <div class='body hide'>
                    <a href="">content1</a>
                    <a href="">content2</a>
                    <a href="">content3</a>
                </div>
            </div>
 
        </div>
 
        <script type="text/javascript">
            $(function(){
                $('.title').click(function(){
                    $(this).parent().siblings().children('.body').addClass('hide');
                    $(this).next().removeClass('hide');
                });
            });
        </script>
    </body>
</html>
效果:



4、tab菜单

css:
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*公共开始*/
body {
    margin0 auto;
    font-familyArial;
    _font-family: 宋体,Arial;
    font-size12px;
}
body, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6precode, form, fieldset, legend, input, button, textarea, p, blockquote, th, td, figure, div {
    margin0;
    padding0;
}
 
ol, ul, li {
    list-stylenone;
}
a{
    cursor:pointer;
    text-decoration:none;
}
/*a:hover{
    color: #F60 !important;
    text-decoration: underline;
}*/
img{
    border:none;
    border-width:0px;
}
table{
    border-collapsecollapse;
    border-spacing0;
}
 
.red{
    color#c00!important;
}
.m8{
    margin:8px;
}
.mt10{
    margin-top:10px;
}
.mt20{
    margin-top:20px;
}
.mr5{
    margin-right:5px;
}
.ml5{
    margin-left:5px;
}
 
.ml10{
    margin-left:10px;
}
 
.mb10{
    margin-bottom:10px;
}
.pt18{
    padding-top:18px;
}
.pt20{
    padding-top:20px;
}
.pb20{
    padding-bottom:20px;
}
.nbr{
    border-right:0px;
}
.font12{
    font-size:12px;
}
.font14{
    font-size:14px;
}
.font16{
    font-size:16px;
}
.bold{
    font-weight:bold;
}
.left{
    float:left;
}
.right{
    float:right;
}
.hide{
    display:none;
}
.show{
     display:table;
}
.clearfix{
    clear:both;
}
.clearfix:after {
    content".";
    displayblock;
    height0;
    clearboth;
    visibilityhidden;
}
* html .clearfix {zoom: 1;}
 
.container{
    width:1190px;
    margin-left:auto;
    margin-right:auto;
 
}
 
.group-box-1 .title{
    height33px;
    line-height33px;
    border1px solid #DDD;
    background#f5f5f5;
    padding-top0;
    padding-left0;
 
}
.group-box-1 .title .title-font{
    display: inline-block;
    font-size14px;
    font-family'Microsoft Yahei','SimHei';
    font-weightbold;
    color#333;
    padding-left10px;
}
.group-box-1 .body {
    border1px solid #e4e4e4;
    border-topnone;
}
 
.tab-menu-box1 {
    border1px solid #ddd;
    margin-bottom20px;
}
 
.tab-menu-box1 .menu {
    line-height33px;
    height33px;
    background-color#f5f5f5;
}
 
.tab-menu-box1 .content {
    min-height100px;
    border-top1px solid #ddd;
    background-colorwhite;
}
 
.tab-menu-box1 .menu ul {
    padding0;
    margin0;
    list-stylenone;
    /*position: absolute;*/
}
 
.tab-menu-box1 .menu ul li {
    positionrelative;
    floatleft;
    font-size14px;
    font-family'Microsoft Yahei','SimHei';
    text-aligncenter;
    font-size14px;
    font-weightbold;
    border-right1px solid #ddd;
    padding0 18px;
    cursorpointer;
}
 
.tab-menu-box1 .menu ul li:hover {
    color#c9033b;
}
 
.tab-menu-box1 .menu .more {
    floatright;
    font-size12px;
    padding-right10px;
    font-family"宋体";
    color#666;
    text-decorationnone;
}
 
.tab-menu-box1 .menu a:hover {
    color#f60 !important;
    text-decorationunderline;
}
 
.tab-menu-box1 .menu .current {
    margin-top-1px;
    color#c9033b;
    background#fff;
    height33px;
    border-top2px solid #c9033b;
    z-index10;
}
 
.tab-menu-box-2 .float-title {
    displaynone;
    top0px;
    positionfixed;
    z-index50;
}
 
.tab-menu-box-2 .title {
    width890px;
    border-bottom2px solid #b20101;
    border-left1px solid #e1e1e1;
    clearboth;
    height32px;
}
 
.tab-menu-box-2 .title a {
    floatleft;
    width107px;
    height31px;
    line-height31px;
    font-size14px;
    font-weightbold;
    text-aligncenter;
    border-top1px solid #e1e1e1;
    border-right1px solid #e1e1e1;
    backgroundurl(/Content/images/bg4.png?30 -308px repeat-x;
    text-decorationnone;
    color#333;
    cursorpointer;
}
 
.tab-menu-box-2 .title a:hover {
    background-position-26px -271px;
    text-decorationnone;
    color#fff;
}
 
.tab-menu-box-2 .content {
    min-height100px;
    background-colorwhite;
}
 
 
.tab-menu-box3 {
    border1px solid #ddd;
}
 
.tab-menu-box3 .menu {
    line-height33px;
    height33px;
    background-color#f5f5f5;
}
 
.tab-menu-box3 .content {
    height214px;
    border-top1px solid #ddd;
    background-colorwhite;
}
 
.tab-menu-box3 .menu ul {
    padding0;
    margin0;
    list-stylenone;
    /*position: absolute;*/
}
 
.tab-menu-box3 .menu ul li {
    positionrelative;
    floatleft;
    font-size14px;
    font-family'Microsoft Yahei','SimHei';
    text-aligncenter;
    font-size14px;
    width:50%;
    cursorpointer;
}
 
.tab-menu-box3 .menu ul li:hover {
    color#c9033b;
}
 
.tab-menu-box3 .menu .more {
    floatright;
    font-size12px;
    padding-right10px;
    font-family"宋体";
    color#666;
    text-decorationnone;
}
 
.tab-menu-box3 .menu a:hover {
    color#f60 !important;
    text-decorationunderline;
    font-weightbold;
}
 
.tab-menu-box3 .menu .current {
 
    margin-top-1px;
    color#c9033b;
    background#fff;
    height33px;
    border-top2px solid #c9033b;
    z-index10;
    font-weightbold;
 
}
 
/*公共结束*/
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
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <link rel="stylesheet" type="text/css" href="common1.css" />
</head>
<body>
    <div class='container'>
        <div class='tab-menu-box1'>
            <div class='menu'>
                <ul id='tab-menu-title'>
                    <li class='current' content-to='1'>价格趋势</li>
                    <li content-to='2'>市场分布</li>
                    <li content-to='3'>其他</li>
                </ul>
            </div>
 
            <div id='tab-menu-body' class='content'>
                <div content='1'>content1</div>
                <div content='2' class='hide'>content2</div>
                <div content='3' class='hide'>content3</div>
            </div>
        </div>
    </div>
    <script src="./jquery-2.1.4.js"></script>
    <script type='text/javascript'>
    $(function(){
        ChangeTab('#tab-menu-title', '#tab-menu-body');
    })
    function ChangeTab(title, body) {
            $(title).children().bind("click", function () {
                $menu = $(this);
                $content = $(body).find('div[content="' + $(this).attr("content-to") + '"]');
                $menu.addClass('current').siblings().removeClass('current');
                $content.removeClass('hide').siblings().addClass('hide');
            });
        }
    </script>
</body>
</html>
效果:


5、滚动菜单

随鼠标滚动左边的menu跟着变化

demo:
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body{
            margin: 0px;
        }
        img {
            border: 0;
        }
        ul{
            padding: 0;
            margin: 0;
            list-style: none;
        }
        .clearfix:after {
            content: ".";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }
        .wrap{
             980px;
            margin: 0 auto;
        }
        .pg-header{
            background-color: #303a40;
            -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);
            -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);
            box-shadow: 0 2px 5px rgba(0,0,0,.2);
        }
        .pg-header .logo{
            float: left;
            padding:5px 10px 5px 0px;
        }
        .pg-header .logo img{
            vertical-align: middle;
             110px;
            height: 40px;
 
        }
        .pg-header .nav{
            line-height: 50px;
        }
        .pg-header .nav ul li{
            float: left;
        }
        .pg-header .nav ul li a{
            display: block;
            color: #ccc;
            padding: 0 20px;
            text-decoration: none;
            font-size: 14px;
        }
        .pg-header .nav ul li a:hover{
            color: #fff;
            background-color: #425a66;
        }
        .pg-body{
 
        }
        .pg-body .catalog{
            position: absolute;
            top:60px;
             200px;
            background-color: #fafafa;
            bottom: 0px;
        }
        .pg-body .catalog.fixed{
            position: fixed;
            top:10px;
        }
 
        .pg-body .catalog .catalog-item.active{
            color: #fff;
            background-color: #425a66;
        }
 
        .pg-body .content{
            position: absolute;
            top:60px;
             700px;
            margin-left: 210px;
            background-color: #fafafa;
            overflow: auto;
        }
        .pg-body .content .section{
            height: 500px;
        }
    </style>
</head>
<body>
    <div class="pg-header">
        <div class="wrap clearfix">
            <div class="logo">
                <a href="#">
                    <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">
                </a>
            </div>
            <div class="nav">
                <ul>
                    <li>
                        <a  href="#">首页</a>
                    </li>
                    <li>
                        <a  href="#">功能一</a>
                    </li>
                    <li>
                        <a  href="#">功能二</a>
                    </li>
                </ul>
            </div>
 
        </div>
    </div>
    <div class="pg-body">
        <div class="wrap">
            <div class="catalog">
                <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
                <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
                <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
            </div>
            <div class="content">
                <div menu="function1" class="section">
                    <h1>第一章</h1>
                </div>
                <div menu="function2" class="section">
                    <h1>第二章</h1>
                </div>
                <div menu="function3" class="section">
                    <h1>第三章</h1>
                </div>
            </div>
        </div>
 
    </div>
 
    <script type="text/javascript" src="jquery-2.1.4.js"></script>
    <script type="text/javascript">
        $(function(){
            Init();
        });
        function Init(){
            $(window).scroll(function() {
                var scrollTop = $(window).scrollTop();
                if(scrollTop > 50){
                    $('.catalog').addClass('fixed');
                }else{
                    $('.catalog').removeClass('fixed');
                }
                $('.content').children().each(function(){
                    var offSet = $(this).offset();
                    var offTop = offSet.top - scrollTop;
                    var height = $(this).height();
 
                    if(offTop<=0 && offTop> -height){
                        //去除其他
                        //添加自己
                        var docHeight = $(document).height();
                        var winHeight = $(window).height();
 
                        if(docHeight == winHeight+scrollTop)
                        {
                            $('.catalog').find('div:last-child').addClass('active').siblings().removeClass('active');
                        }else{
                            var target = $(this).attr('menu');
                            $('.catalog').find('div[auto-to="'+target+'"]').addClass('active').siblings().removeClass('active');
                        }
 
 
                    }
                });
 
            });
 
 
        }
 
    </script>
</body>
</html>
效果:


6、登录注册

下载地址:http://files.cnblogs.com/files/wupeiqi/%E7%99%BB%E9%99%86%E6%B3%A8%E5%86%8C.zip

7、更多实例

下载地址:http://files.cnblogs.com/files/wupeiqi/HtmlStore.zip



部分来源:http://www.cnblogs.com/wupeiqi/






原文地址:https://www.cnblogs.com/daliangtou/p/5201771.html