JQuery

1.JQuery加载:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery加载</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">
        <!--比原生的快,完整写法-->
        $(document).ready(function () {
            var $div= $('#div');
            alert($div.html()+'jQuery');
        })

    //    简写方法
        $(function () {
            var $div= $('#div');
            alert($div.html()+'jQuery');
        })

    </script>
</head>
<body>
<div id="div">z这是一个div</div>
</body>
</html>

2.JQuery选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery选择器</title>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">

        $(function () {
            $('div1').css({color:'pink'});
            $('.box').css({fontSize:'30px'});
            $('.list li').css({backgroundColor:'green',color:'#fff',fontSize:'20px'});
        })
    </script>
    <style type="text/css">
        #div1{
            color: red;
        }

        .box{
            color: green;
        }

        .list li{
            background-color: gold;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div id="div1">这是一个div元素</div>
<div class="box">这是第二个div</div>
<ul class="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>
</body>
</html>

3.JQuery选择集转移

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery选择集转移</title>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {

            // next()同级下的下一个元素  nextAll同级下的所有元素
            $('#div1').nextAll('p').css({backgroundColor:'green'});

            // prev()同级上的下一个元素  prevAll同级上的所有元素


            //parent选择上一级父级元素
            $('#span01').parent().css({width:'100px',height:'100px',
                backgroundColor:'red'});

            //选择最近的一个元素
            $('#span02').closest('div').css({width:'200px',height:'200px',
            backgroundColor:'pink'});

            //$(.list li)不能回到父级
            //$('.list).children():可以通过end()回到父级
            $('.list').children().css({backgroundColor:'gold',height:'30px',
            marginBottom:'10px'}).end().css({backgroundColor:'green'});

            //选中除了第三个元素其他的同级li,反选
            $('.list li:eq(2)').css({background:'gold'}).siblings().css({background:'green'});


            //找到div4内的a元素
            $('#div4').find('.link1').css({color:'red'});

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

<div id="div1">这是一个div</div>
<div id="div2">这是第二个div</div>
<p>这是一个p元素</p>

<div id="div3">
    <a href="#">百度网</a>

    <span id="span01">span元素</span>
</div>

<div id="div4">
    <p>
        <a href="#" class="link1">百度网</a>

        <span id="span02">span元素</span>
    </p>

</div>

<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>

4.JQuery样式操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery样式操作</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">
        $(function () {

            //读取样式
            alert($('.div1').css('fontSize'));


        //    设置样式
            $('.div1').css({background:'gold'});

            //添加一个样式
            $('.div1').addClass('big');

            $('.div1').removeClass('div1');
        })
    </script>

    <style type="text/css">
        .big{
            font-size: 30px;
        }
    </style>
</head>
<body>
<div class="div1">这是一个div</div>
</body>
</html>

5.JQuery属性操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery属性操作</title>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {

            //读写元素内容
            alert($('.box').html());
            $('.box').html('<a href="http://www.baidu.com">百度</a>');


            //读写元素内部title属性
            alert($('.box').attr('title'));
            $('.box').attr({title:'这是一个div'});


            //读写checkBox的属性
            alert($('#checkBox').prop('checked'));
            $('#checkBox').prop({checked:false});


            //text属性
            alert($('.box2').text);
        })

    </script>


    <style type="text/css">

    </style>
</head>
<body>
<div class="box" title="hahaha">这是一个div</div>
<input type="checkbox" name="" id="checkBox" checked>多选


<div class="box2"><span>这是一个span</span></div>
</body>
</html>

6.JQuery 绑定click事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JQuery 绑定click事件</title>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">
        $(function () {
            $('#btn').click(function () {
                $('.box').toggleClass('sty');
            })
        })


    </script>

    <style type="text/css">
        .box{
            width: 200px;
            height: 200px;
            background-color: gold;
        }
        .sty{
            background-color: red;
        }
    </style>
</head>
<body>
<input type="button" id="btn" value="切换">
<div class="box"></div>
</body>
</html>

7.JQuery制作选项卡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery制作选项卡</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.btns input').click(function () {

                //this表示当前点击的对象
                $(this).addClass('cur').siblings().removeClass('cur');
                //$(this).index()获取当前按钮所在层级的索引值
                $('#contents div').eq($(this).index()).addClass('active').siblings()
                    .removeClass('active');


            })
        })
    </script>

    <style type="text/css">
        .btns{
            width: 500px;
            height: 50px;
        }

        .btns input{
            width: 100px;
            height: 50px;
            margin: 0px 0;
            padding: 0px 0;
            border: none ;
            background-color: #dddddd;
            color: #ffffff;
        }

        .btns input.cur{
            background-color: #4ab14e;
        }

        .contents div{
            width: 300px;
            height: 200px;
            background-color: #4ab14e;
            display: none;
            line-height: 300px;
            text-align: center;
        }

        .contents .active{
            display: block;
        }

    </style>
</head>
<body>
<div class="btns" id="btns">
    <input type="button"  value="btn01" class="cur">
    <input type="button"  value="btn02" >
    <input type="button"  value="btn03" >
</div>

<div class="contents" id="contents">

    <div class="active">第一页</div>
    <div>第二页</div>
    <div>第三页</div>
</div>

</body>
</html>

8.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;
        }
    </style>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">

        $(function () {
            $('#btn').click(function () {
                //淡出
                // $('.box').fadeOut();

                //切换淡入淡出
                // $('.box').fadeToggle();

                //切换显示/隐藏
                // $('.box').toggle();

                //切换上下进入
                $('.box').slideToggle();


            })
        })
    </script>
</head>
<body>
<input type="button" name="" value="效果" id="btn">
<div class="box"></div>
</body>
</html>

9.jquery层级菜单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery层级菜单</title>
    <style type="text/css">
        body{
            font-family: 'Microsoft Yahei';
        }
        body,ul{
            margin: 0;
            padding: 0;
        }

        ul{
            list-style:none;
        }

        .menu{
            width: 200px;
            margin: 20px auto 0;
        }

        .menu .level1,.menu li ul a{
            display: block;
            width: 200px;
            height: 30px;
            line-height: 30px;
            text-decoration: none;
            background-color: #3366cc;
            color: #ffffff;
            font-size: 16px;
            text-indent: 10px;
        }

        .menu .level1{
            border-bottom: 1px solid #afc6f6;
        }

        .menu li ul a{
            font-size: 14px;
            text-indent: 20px;
            background-color: #7aa1ef;
        }

        .menu li ul li{
            border-bottom: 1px solid #afc6f6;
        }

        .menu li ul{
            display: none;
        }

        .menu li ul.current{
            display: block;
        }

        .menu li ul li a:hover{
            background-color: #f6b544;
        }
    </style>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">

        $(function () {
            $('.level1').click(function () {
                // $(this).next().toggleClass('current');
                // $(this).next().slideToggle();

                $(this).next().slideDown().parent().siblings().children('ul')
                    .slideUp();
            })
        })
    </script>
</head>
<body>
<ul class="menu">
    <li>
        <a href="#" class="level1">水果</a>
        <ul class="current">
            <li><a href="#">苹果</a></li>
            <li><a href="#">梨子</a></li>
            <li><a href="#">葡萄</a></li>
            <li><a href="#">火龙果</a></li>
        </ul>
    </li>
    <li>
        <a href="#" class="level1">海鲜</a>
        <ul>
            <li><a href="#">蛏子</a></li>
            <li><a href="#">扇贝</a></li>
            <li><a href="#">龙虾</a></li>
            <li><a href="#">象拔蚌</a></li>
        </ul>
    </li>
    <li>
        <a href="#" class="level1">肉类</a>
        <ul>
            <li><a href="#">内蒙古羊肉</a></li>
            <li><a href="#">进口牛肉</a></li>
            <li><a href="#">野猪肉</a></li>
        </ul>
    </li>
    <li>
        <a href="#" class="level1">蔬菜</a>
        <ul>
            <li><a href="#">娃娃菜</a></li>
            <li><a href="#">西红柿</a></li>
            <li><a href="#">西芹</a></li>
            <li><a href="#">胡萝卜</a></li>
        </ul>
    </li>
    <li>
        <a href="#" class="level1">速冻</a>
        <ul>
            <li><a href="#">冰淇淋</a></li>
            <li><a href="#">湾仔码头</a></li>
            <li><a href="#">海参</a></li>
            <li><a href="#">牛肉丸</a></li>
        </ul>
    </li>

</ul>
</body>
</html>

10.jquery动画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery动画</title>

    <style type="text/css">
        .box{
            width: 100px;
            height: 100px;
            background-color: gold;
        }

    </style>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {

            //属性设置
            // ({param1:1,param2:2},
            // time,
            // 动画曲线(swing||liner)')
            // 回调函数
            $('#div1').animate({width:200,height:200},3000,'linear',function () {
                $(this).animate({marginLeft:500},1000);
            });


        })
    </script>
</head>
<body>
<div id="div1" class="box">

</div>
</body>
</html>

11.手风琴效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery实现手风琴效果</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>

    <script type="text/javascript">

    </script>

    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            font-size:12px;
        }
        #accordion{
            width:727px; height:350px; margin:100px auto 0 auto; position:relative; overflow:hidden; border:1px solid #CCC;
        }
        #accordion ul{
            list-style:none;
        }
        #accordion ul li{width:643px;height:350px; position:absolute; background:#FFF;}
        #accordion ul li span{display:block;width:20px; height:350px; float:left; text-align:center; color:#FFF; padding-top:5px; cursor:pointer;}
        #accordion ul li img{display:block; float:right;}
        .bar01{left:0px;}
        .bar02{left:643px;}
        .bar03{left:664px;}
        .bar04{left:685px;}
        .bar05{left:706px;}
        .bar01 span{background:#09E0B5;}
        .bar02 span{background:#3D7FBB;}
        .bar03 span{background:#5CA716;}
        .bar04 span{background:#F28B24;}
        .bar05 span{background:#7C0070;}
    </style>

    <script type="text/javascript">

        $(function(){

            $('#accordion li').click(function() {

                $(this).animate({left:$(this).index()*21});

                $(this).prevAll().each(function(){

                    $(this).animate({left:$(this).index()*21});

                });

                $(this).nextAll().each(function(){

                    $(this).animate({left:(727-(5-$(this).index())*21)});
                });


            });


        })


    </script>
</head>
<body>
<div id="accordion">
    <ul>
        <li class="bar01"><span>非洲景色01</span><img src="image/001.jpg" /></li>
        <li class="bar02"><span>非洲景色02</span><img src="image/002.jpg" /></li>
        <li class="bar03"><span>非洲景色03</span><img src="image/003.jpg" /></li>
        <li class="bar04"><span>非洲景色04</span><img src="image/004.jpg" /></li>
        <li class="bar05"><span>非洲景色05</span><img src="image/005.jpg" /></li>
    </ul>
</div>
</body>
</html>

12.jquery循环方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery循环</title>
    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.list li').each(function (index) {
                $(this).html(index);
            })
        })

    </script>
</head>
<body>
<ul class="list">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>
</body>
</html>

13.jquery元素相对位置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery元素绝对位置</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function () {
            var $pos=$('.pos');
            var pos=$pos.offset();//获取元素相对页面的绝对位置
            var w=$pos.outerWidth();//padding+border+width
            var h=$pos.outerHeight();//padding+border+height

            $('.pop').css({left:pos.left+w,top:pos.top});//将提示框放在li3的右边

            //鼠标进入/移出事件,进入子元素时会触发
            $pos.mouseover(function () {
                $('.pop').show();
            });
            $pos.mouseout(function () {
                $('.pop').hide();
            });



            //鼠标进入/移出事件,进入子元素时不会触发
            $pos.mouseenter(function () {
                $('.pop').show();
            });
            $pos.mouseleave(function () {
                $('.pop').hide();
            });
            
            
        })
    </script>

    <style>
        .con{
            width: 600px;
            height: 600px;
            margin: 50px auto 0;
        }

        .box{
            width: 100px;
            height: 100px;
            background-color: gold;
            margin-bottom: 10px;
        }

        .pos{
            margin-left: 500px;
        }

        .pop{
            width: 100px;
            height: 100px;
            background-color: red;
            position: fixed;
            left: 0;
            display: none;
        }

    </style>
</head>
<body>
<div class="con">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box pos"></div>
    <div class="box"></div>
</div>
<div class="pop">提示信息</div>
</body>
</html>

14.jquery自定义事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery自定义事件</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">
        
        $(function () {
            
            $('#btn').bind('hello',function () {
                alert('hello');
            });
            
            $('#btn').bind('click',function () {
                alert('click');
            });
            
            
            $('#btn2').click(function () {
                $('#btn').trigger('hello');
                $('#btn').trigger('click');
            })
        })
        
    </script>
</head>
<body>
<input type="button" name="" value="按钮" id="btn">
<input type="button" name="" value="按钮2" id="btn2">
</body>
</html>

15.事件冒泡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery事件冒泡</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script type="text/javascript">

        $(function () {
            $('body').click(function () {
                alert(4);
            })

            $('.grandfather').click(function () {
                alert(3);
            })

            $('.father').click(function () {
                alert(2);
            })

            $('.son').click(function () {
                alert(1);
                //结束事件传递
                return false;
            })
        })
    </script>

    <style>
        .grandfather{
            width: 300px;
            height: 300px;
            background-color: green;
            position: relative;
        }


        .father{
            width: 200px;
            height: 200px;
            background-color: gold;
        }

        .son{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 400px;
        }
    </style>
</head>
<body>
<div class="grandfather">
    <div class="father">
        <div class="son">

        </div>
    </div>
</div>
</body>
</html>

16.jquery阻止事件冒泡(提示框)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery阻止事件冒泡(提示框)</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script>

        $(function () {
            $('#btn').click(function () {
                $('#pop').show();
                return false;
            })

            $('#shutoff').click(function () {
                $('#pop').hide();
            })

            $(document).click(function () {
                $('#pop').hide();
            })

            $('.pop').click(function () {
                return false;
            })
        })
    </script>

    <style>
        .pop_con{
            display: none;
        }

        .pop{
            width: 400px;
            height: 300px;
            background-color: #fff;
            border-right: 1px solid #000;
            position: fixed;
            left: 50%;
            top: 50%;
            margin-left: -200px;
            margin-top: -150px;
            z-index: 9999;
        }

        .mask{
            position: fixed;
            width: 100%;
            height: 100%;
            background-color: #000000;
            left: 0;
            top: 0;
            opacity: 0.3;
            filter: alpha(opacity=30);
            z-index: 9990;
        }
    </style>
</head>
<body>
<h1>首页标题</h1>
<p>页面内容</p>

<input type="button" name="" value="弹出" id="btn">

<div class="pop_con" id="pop">
    <div class="pop">
        <h3>提示信息</h3>
        <a href="#" id="shutoff">关闭</a>
        <input type="text" name="">
    </div>

    <div class="mask"></div>
</div>
</body>
</html>

17.JQuery ListDemo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ListDemo</title>

    <script type="text/javascript" src="jquery-1.12.4.js"></script>
    <script>

        $(function () {
            $('#btn1').click(function () {
                var txt=$('#txt1').val();
                $('#txt1').val('');

                if (txt == '') {
                    alert('请输入内容!');
                    return;
                }

                $li=$('<li><span>'+txt+'</span><a href="javascript:;" class="up">↑</a>' +
                    '<a href="javascript:;" class="down">↓</a>' +
                    '<a href="javascript:;" class="del">删除</a></li>');
                $li.appendTo('#list');
            });

            $('#list').delegate('a','click',function () {
                var handler=$(this).attr('class');
                if (handler=='del'){
                    $(this).parent().remove();
                }

                if (handler == 'up') {
                    if ($(this).parent().prev().length == 0) {
                        alert('到顶了');
                        return;
                    }
                    $(this).parent().insertBefore($(this).parent().prev());
                }

                if (handler == 'down') {
                    if ($(this).parent().next().length==0){
                        alert('到底了');
                    }
                    $(this).parent().insertAfter($(this).parent().next());
                }

            })
        })
    </script>

    <style>
        .list_con{
            width: 400px;
            margin: 50px auto 0;
        }

        .input1{
            width: 350px;
            height: 30px;
            border: 1px solid #ccc;
            padding: 0px;
            text-indent: 10px;
        }
        .btn1{
            width: 40px;
            height: 32px;
            padding: 0px;
            border: 1px solid #ccc;
        }

        .list{
            margin: 0;
            padding: 0;
            list-style: none;
            margin-top: 20px;
        }

        .list li{
            height: 30px;
            line-height: 30px;
            border-bottom: 1px solid #ccc;
        }

        .list li span{
            float: left;
        }

        .list li a{
            float: right;
            text-decoration: none;
            margin: 0 10px;
        }

    </style>
</head>
<body>

<div class="list_con">

    <h2>List Demo</h2>
    <input type="text" name="" id="txt1" class="input1">
    <input type="button" name="" id="btn1" value="增加" class="btn1">


    <ul class="list" id="list">
        <li>
            <span>学习html</span>
            <a href="javascript:;" class="up">↑</a>
            <a href="" class="down">↓</a>
            <a href="" class="del">删除</a>
        </li>
        <li>
            <span>学习css</span>
            <a href="javascript:;" class="up">↑</a>
            <a href="" class="down">↓</a>
            <a href="" class="del">删除</a>
        </li>
        <li>
            <span>学习js</span>
            <a href="javascript:;" class="up">↑</a>
            <a href="" class="down">↓</a>
            <a href="" class="del">删除</a>
        </li>
    </ul>

</div>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值