黑马程序员技术交流社区

标题: 【上海校区】click事件/jQuery索引值/ jQuery做选项卡/ jQuery属... [打印本页]

作者: 不二晨    时间: 2019-1-18 09:19
标题: 【上海校区】click事件/jQuery索引值/ jQuery做选项卡/ jQuery属...
click事件

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>click事件</title>
        <style type="text/css">
                .box{
                        width: 200px;
                        height: 200px;
                        background-color: gold;
                }
                .sty{
                        background-color: green;
                }
        </style>
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
                $(function(){
                        // 给按钮绑定click事件
                        $('#btn').click(function(){
                                //重复切换sty样式
                                $('.box').toggleClass('sty');
                        })
                })
        </script>
</head>
<body>
        <input type="button" value="切换" id="btn">
        <div class="box"></div>
</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
jQuery索引值

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>jQuery索引值</title>
        <style type="text/css">
                .list li{
                        height: 30px;
                        margin-bottom: 10px;
                        background-color: gold;
                }
        </style>
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
                $(function(){
                        $('.list li').click(function(){
                                // alert(this.innerHTML);//弹出标签中的内容
                                alert($(this).index());//弹出下标
                        })
                })
        </script>
</head>
<body>
        <ul class="list">
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
                <li>8</li>
        </ul>
</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
jQuery做选项卡

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>jQuery做选项卡</title>
        <style type="text/css">
                .btns{
                        width: 500px;
                        height: 50px;
                }
                /*选项卡的样式*/
                .btns input{
                        width: 100px;
                        height: 50px;
                        background-color: #ddd;/*默认灰色*/
                        color: #666;
                        border: 0px;
                }
                /*被选中的选项卡的样式*/
                .btns input.cur{
                        background-color: gold;
                }
                /*内容区的样式*/
                .contents div{
                        width: 500px;
                        height: 300px;
                        background-color: gold;
                        display: none;/*默认隐藏*/
                        line-height: 300px;
                        text-align: center;
                }
                /*被选中的内容区的样式*/
                .contents div.active{
                        display: block;
                }
        </style>
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
                $(function(){
                        $('#box1 #btns input').click(function() {
                                //失去焦点,避免出现默认的蓝框
                                $(this).blur();
                                //this是原生的对象
                                // alert(this);//弹出[object HTMLInputElement],说明this就是当前点击的input元素

                                //jQuery的this对象使用时要用$()包起来,这样就可以调用jQuery的方法了
                                //给当前元素添加选中样式,为兄弟元素移除选中样式
                                $(this).addClass('cur').siblings().removeClass('cur');

                                //$(this).index()获取当前按钮所在层级范围的索引值
                                //显示对应索引的内容区,隐藏其它兄弟内容区
                                $('#box1 #contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');
                        });
                       
                        $('#box2 #btns input').click(function() {
                                $(this).blur();
                                $(this).addClass('cur').siblings().removeClass('cur');
                                $('#box2 #contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');
                        });
                })
        </script>
</head>
<body>
        <div id="box1">
                <div class="btns" id="btns">
                        <input type="button" value="tab01" class="cur">
                        <input type="button" value="tab02">
                        <input type="button" value="tab03">
                </div>
        </div>
        <br><br>
        <div id="box2">
                <div class="contents" id="contents">
                        <div class="active">tab文字内容一</div>
                        <div>tab文字内容二</div>
                        <div>tab文字内容三</div>
                </div>
        </div>
</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
jQuery属性操作

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>jQuery属性操作</title>
        <style type="text/css">
               
        </style>
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
                $(function(){
                        /*
                        alert($('.box').html());//这是一个div元素
                        $('.box').html('<a href="http://www.baidu.com">百度网</a>');
                        */

                        /*
                        读写值为布尔类型的属性用prop方法
                        读写值为非布尔类型的属性用attr方法
                        */

                        /*
                        $('.box').attr({title:'这是一个div!'});//写入title属性,并赋值
                        alert($('.box').attr('class'));//读属性class的值,弹出box
                        */

                        /*
                        var $src = $('#img1').attr('src');
                        alert($src);//img/1.png

                        $('#img1').attr({
                                src:'img/2.gif',
                                alt:'图片二'
                        });
                        */

                        /*
                        alert($('#check').prop('checked'));//选中为true,非选中为false
                        $('#check').prop({checked:true});//设置默认勾选
                        */

                        // alert($('.box2').html());//<span>这是div元素内的span</span>
                        alert($('.box2').text());//这是div元素内的span
                })
        </script>
</head>
<body>
        <div class="box">这是一个div元素</div>

        <img id="img1" src="img/1.png" alt="一张图片">

        <input type="checkbox" id="check">多选

        <div class="box2">
                <span>这是div元素内的span</span>
        </div>
</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
jQuery特殊效果

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>jQuery特殊效果</title>
        <style type="text/css">
                .box{
                        width: 200px;
                        height: 200px;
                        background-color: gold;
                        display: none;
                }
        </style>
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
                $(function(){
                        $('#btn').click(function(){
                                // $('.box').fadeOut();//淡出
                                // $('.box').fadeIn();//淡入
                                // $('.box').fadeToggle();//切换淡入淡出
                                // $('.box').toggle();//切换显示隐藏
                                $('.box').slideToggle();//切换上收和下展
                        })
                })
        </script>
</head>
<body>
        <input type="button" name="" value="效果" id="btn">
        <div class="box"></div>
</body>
</html>
---------------------
【转,仅作分享,侵删】
作者:YRyr.*
原文:https://blog.csdn.net/weixin_43152725/article/details/86483184



作者: 不二晨    时间: 2019-1-23 17:27
奈斯,感谢分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2