前端基础系列(七)JQuery基础(上篇)

一、使用JQ弹出广告

(1)JQ入门:


<html>
    <head>
        <meta charset="UTF-8">
        <title>jquery入门title>
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                alert("hello jquery!");
            });
        script>
    head>
    <body>
    body>
html>

运行结果:
前端基础系列(七)JQuery基础(上篇)_第1张图片
(2)JS与JQ页面加载区别:


<html>
    <head>
        <meta charset="UTF-8">
        <title>JS与JQ页面加载区别title>
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            window.onload = function(){
                alert("张三");
            }

            //传统的方式页面加载会存在覆盖问题,加载比JQ慢(整个页面加载完毕<包括里面的其它内容,比如图片>)
            window.onload = function(){
                alert("老王");
            }

            //JQ的加载比JS加载要快!(当整个dom树结构绘制完毕就会加载)
            jQuery(document).ready(function(){
                alert("李四");
            });

            //JQ不存在覆盖问题,加载的时候是顺序执行
            $(document).ready(function(){
                alert("王五");
            });

            //简写方式
            $(function(){
                alert("汾九");
            });

        script>
    head>
    <body>
    body>
html>

运行结果依次显示:李四 王五 汾九 老王
(3)JQ获取:


<html>
    <head>
        <meta charset="UTF-8">
        <title>JQ的获取title>
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                //1.JS方式获取
                /*document.getElementById("btn").οnclick= function(){
                    location.href="惊喜.html";
                }*/
                //2.JQ方式获取=====>>>$("#btn")
                $("#btn").click(function(){
                    location.href="惊喜.html"
                });
            });
        script>
    head>
    <body>
        <input type="button" value="点我有惊喜" id="btn"/>
    body>
html>

(4)DOM和JQ对象的转换:


<html>
    <head>
        <meta charset="UTF-8">
        <title>Dom与JQ对象之间的转换title>
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            function write1(){
                //1.JS的DOM操作
                //document.getElementById("span1").innerHTML="萌萌哒!";
                //DOM对象无法操作JQ对象里面属性和方法
                //document.getElementById("span1").html("萌萌哒!");

                var spanEle = document.getElementById("span1");

                //将DOM对象转换成JQ对象
                $(spanEle).html("思密达");
            }
            $(function(){
                $("#btn").click(function(){
                    //JQ对象无法操作JS里面的属性和方法!!!
                    //$("#span1").innerHTML="呵呵哒!"
                    $("#span1").html("呵呵哒!");

                    //JQ对象向DOM对象转换方式一
                    $("#span1").get(0).innerHTML="美美哒!";

                    //JQ对象向DOM对象转换方式二
                    $("#span1")[0].innerHTML="棒棒哒!";

                });
            });
        script>
    head>
    <body>
        <input type="button" value="JS写入" onclick="write1()"/>
        <input type="button" value="JQ写入" id="btn"/><br />
        班长:<span id="span1">你好帅!span>
    body>
html>

运行结果:
点击JS写入,显示班长斯密达;点击JQ写入,显示班长棒棒哒
(5)toggle使用:


<html>
    <head>
        <meta charset="UTF-8">
        <title>toggle的使用title>
        <style>
            div{
                border: 1px solid white;
                width: 500px;               
                height: 350px;
                margin: auto;
                text-align: center;
            }
        style>
        <script type="text/javascript" src="../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                $("#btn").click(function(){
                    $("#img1").toggle();
                });
            });
        script>
    head>
    <body>
        <div>
            <input type="button" value="显示/隐藏" id="btn" /><br />|
            <img src="../img/飞机05.gif" width="100%" height="100%" id="img1"/>
        div>
    body>
html>

运行结果:点击按钮之后图片隐藏,再次点击的时候图片显示
(6)首页广告显示:


<html>
    <head>
        <meta charset="UTF-8">
        <title>首页title>
        <style>
            #father{
                border: 0px solid red;
                width: 1300px;
                height: 2170px;
                margin: auto;
            }
            /*#logo{
                border: 0px solid black;
                width: 1300px;
                height: 50px;
            }*/
            .top{
                border: 0px solid blue;
                width: 431px;
                height: 50px;
                float: left;
            }
            #top{
                padding-top: 12px;
                height: 38px;
            }
            #menu{
                border: 0px solid red;
                width: 1300px;
                height: 50px;
                background-color: black;
                margin-bottom: 10px;
            }
            ul li{
                display: inline;
                color: white;
            }
            #clear{
                clear: both;
            }

            #product{
                border: 0px solid red;
                width: 1300px;
                height: 558px;
            }
            #product_top{
                border: 0px solid blue;
                width: 100%;
                height: 45px;
                padding-top: 8px;
            }
            #product_bottom{
                border: 0px solid green;
                width: 100%;
                height: 500px;
            }
            #product_bottom_left{
                border: 0px solid red;
                width: 200px;
                height: 500px;
                float: left;
            }
            #product_bottom_right{
                border: 0px solid blue;
                width: 1094px;
                height: 500px;
                float: left;
            }
            #big{
                border: 0px solid red;
                width: 544px;
                height: 248px;
                float: left;
            }
            .small{
                border: 0px solid blue;
                width: 180px;
                height: 248px;
                float: left;
                /*让里面的内容居中*/
                text-align: center;
            }

            #bottom{
                text-align: center;
            }

            a{
                text-decoration: none;
            }
        style>

        <script type="text/javascript" src="../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                //1.书写显示广告图片的定时操作
                time = setInterval("showAd()",3000);
            });

            //2.书写显示广告图片的函数
            function showAd(){
                //3.获取广告图片,并让其显示
                //$("#img2").show(1000);
                //$("#img2").slideDown(5000);
                $("#img2").fadeIn(4000);
                //4.清除显示图片定时操作
                clearInterval(time);
                //5.设置隐藏图片的定时操作
                time = setInterval("hiddenAd()",3000);
            }

            function hiddenAd(){
                //6.获取广告图片,并让其隐藏
                //$("#img2").hide();
                //$("#img2").slideUp(5000);
                $("#img2").fadeOut(6000);
                //7.清除隐藏图片的定时操作
                clearInterval(time);
            }
        script>

    head>
    <body onload="init()">
        <div id="father">
            
            <img src="../img/f001a62f-a49d-4a4d-b56f-2b6908a0002c_g.jpg" width="100%" style="display: none" id="img2"/>

            
            <div id="logo">
                <div class="top">
                    <img src="../img/logo2.png" height="46px"/>
                div>
                <div class="top">
                    <img src="../img/header.png" height="46px" />
                div>
                <div class="top" id="top">
                    <a href="#">登录a>
                    <a href="#">注册a>
                    <a href="#">购物车a>
                div>
            div>
            <div id="clear">

            div>
            
            <div id="menu">
                <ul>
                    <a href="#"><li style="font-size: 20px;">首页li>a>    
                    <a href="#"><li>手机数码li>a>    
                    <a href="#"><li>家用电器li>a>    
                    <a href="#"><li>鞋靴箱包li>a>    
                    <a href="#"><li>孕婴保健li>a>    
                    <a href="#"><li>奢侈品li>a>
                ul>
            div>
            
            <div id="">
                <img src="../img/1.jpg" width="100%" id="img1"/>
            div>
            
            <div id="product">
                <div id="product_top">
                       
                    <span style="font-size: 25px;padding-top: 8px;">最新商品span>   
                    <img src="../img/title2.jpg" />
                div>
                <div id="product_bottom">
                    <div id="product_bottom_left">
                        <img src="../img/big01.jpg" width="100%" height="100%"/>
                    div>
                    <div id="product_bottom_right">
                        <div id="big">
                            <a href="#"><img src="../img/middle01.jpg" width="100%" height="100%"/>a>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                    div>
                div>
            div>
            
            <div id="">
                <img src="../img/ad.jpg" width="100%"  />
            div>
            
            <div id="product">
                <div id="product_top">
                       
                    <span style="font-size: 25px;padding-top: 8px;">热门商品span>   
                    <img src="../img/title2.jpg" />
                div>
                <div id="product_bottom">
                    <div id="product_bottom_left">
                        <img src="../img/big01.jpg" width="100%" height="100%"/>
                    div>
                    <div id="product_bottom_right">
                        <div id="big">
                            <a href="#"><img src="../img/middle01.jpg" width="100%" height="100%"/>a>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                        <div class="small">
                            <img src="../img/small03.jpg" />
                            <a href="#"><p style="color: gray;">电炖锅p>a>
                            <p style="color: red;">¥399p>
                        div>
                    div>
                div>
            div>
            
            <div id="">
                <img src="../img/footer.jpg" width="100%"/>
            div>
            
            <div id="bottom">
                <a href="#">关于我们a>
                <a href="#">联系我们a>
                <a href="#">招贤纳士a>
                <a href="#">法律声明a>
                <a href="#">友情链接a>
                <a href="#">支付方式a>
                <a href="#">配送方式a>
                <a href="#">服务声明a>
                <a href="#">广告声明a>
                <p>
                    Copyright © 2005-2018 火之意志 版权所有 
                p>
            div>
        div>
    body>
html>

二、JQ选择器

(1)基本选择器:


<html>
    <head>
        <meta charset="UTF-8">
        <title>基本选择器title>
        <link rel="stylesheet" href="../../css/style.css" />
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                $("#btn1").click(function(){
                    $("#one").css("background-color","pink");
                });

                $("#btn2").click(function(){
                    $(".mini").css("background-color","pink");
                });

                $("#btn3").click(function(){
                    $("div").css("background-color","pink");
                });

                $("#btn4").click(function(){
                    $("*").css("background-color","pink");
                });

                $("#btn5").click(function(){
                    $("#two,.mini").css("background-color","pink");
                });
            });
        script>

    head>
    <body>
        <input type="button" id="btn1" value="选择为one的元素"/>
        <input type="button" id="btn2" value="选择样式为mini的元素"/>
        <input type="button" id="btn3" value="选择所有的div元素"/>
        <input type="button" id="btn4" value="选择所有元素"/>
        <input type="button" id="btn5" value="选择id为two并且样式为mini的元素"/>
        <hr/>
        <div id="one">
            <div class="mini">
                111
            div>
        div>

        <div id="two">
            <div class="mini">
                222
            div>
            <div class="mini">
                333
            div>
        div>

        <div id="three">
            <div class="mini">
                444
            div>
            <div class="mini">
                555
            div>
            <div class="mini">
                666
            div>
        div>

        <span id="four">

        span>
    body>
html>

(2)层级选择器:


<html>
    <head>
        <meta charset="UTF-8">
        <title>层级选择器title>
        <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script type="text/javascript">
            $(function(){
                $("#btn1").click(function(){
                    $("body div").css("background-color","gold");
                });

                $("#btn2").click(function(){
                    $("body>div").css("background-color","gold");
                });

                $("#btn3").click(function(){
                    $("#two+div").css("background-color","gold");
                });

                $("#btn4").click(function(){
                    $("#one~div").css("background-color","gold");
                });
            });
        script>


    head>
    <body>
        <input type="button" id="btn1" value="选择body中的所有的div元素"/>
        <input type="button" id="btn2" value="选择body中的第一级的孩子"/>
        <input type="button" id="btn3" value="选择id为two的元素的下一个元素"/>
        <input type="button" id="btn4" value="选择id为one的所有的兄弟元素"/>

        <hr/>
        <div id="one">
            <div class="mini">
                111
            div>
        div>

        <div id="two">
            <div class="mini">
                222
            div>
            <div class="mini">
                333
            div>
        div>

        <div id="three">
            <div class="mini">
                444
            div>
            <div class="mini">
                555
            div>
            <div class="mini">
                666
            div>
        div>

        <span id="four">

        span>
    body>
html>

(3)属性选择器:


<html>
    <head>
        <meta charset="UTF-8">
        <title>层级选择器title>
        <link rel="stylesheet" href="../../css/style.css" />
        <script type="text/javascript" src="../../js/jquery-1.8.3.js" >script>
        <script>
            $(function(){
                $("#btn1").click(function(){
                    $("div[id]").css("background-color","red");
                });

                $("#btn2").click(function(){
                    $("div[id='two']").css("background-color","red");
                });

            });
        script>
    head>
    <body>
        <input type="button" id="btn1" value="选择有id属性的div"/>
        <input type="button" id="btn2" value="选择有id属性的值为two的div"/>

        <hr/>
        <div id="one">
            <div class="mini">
                111
            div>
        div>
        <div id="two">
            <div class="mini">
                222
            div>
            <div class="mini">
                333
            div>
        div>

        <div id="three">
            <div class="mini">
                444
            div>
            <div class="mini">
                555
            div>
            <div class="mini">
                666
            div>
        div>

        <span id="four">

        span>
    body>
html>

三、使用JQ弹隔行换色


<html>
    <head>
        <meta charset="UTF-8">
        <title>使用jQuery完成表格隔行换色title>
        <link rel="stylesheet" href="../css/style.css" />
        <script type="text/javascript" src="../js/jquery-1.8.3.js" >script>
        <script>
            //1.页面加载
            $(function(){
                /*//2.获取tbody下面的偶数行并设置背景颜色
                $("tbody tr:even").css("background-color","yellow");
                //3.获取tbody下面的奇数行并设置背景颜色
                $("tbody tr:odd").css("background-color","green");*/

                //2.获取tbody下面的偶数行并设置背景颜色
                $("tbody tr:even").addClass("even");
                $("tbody tr:even").removeClass("even");
                //3.获取tbody下面的奇数行并设置背景颜色
                $("tbody tr:odd").addClass("odd");
            });
        script>

    head>
    <body>
        <table border="1" width="500" height="50" align="center" id="tbl" id="tbl">
            <thead>
                <tr>
                    <th>编号th>
                    <th>姓名th>
                    <th>年龄th>
                tr>
            thead>
            <tbody>
                <tr >
                    <td>1td>
                    <td>张三td>
                    <td>22td>
                tr>
                <tr >
                    <td>2td>
                    <td>李四td>
                    <td>25td>
                tr>
                <tr >
                    <td>3td>
                    <td>王五td>
                    <td>27td>
                tr>
                <tr >
                    <td>4td>
                    <td>赵六td>
                    <td>29td>
                tr>
                <tr >
                    <td>5td>
                    <td>田七td>
                    <td>30td>
                tr>
                <tr >
                    <td>6td>
                    <td>汾九td>
                    <td>20td>
                tr>
            tbody>
        table>
    body>
html>

运行结果:
前端基础系列(七)JQuery基础(上篇)_第2张图片

四、使用JQ全选/全部选


<html>
    <head>
        <meta charset="UTF-8">
        <title>使用jQuery完成复选框的全选和全不选title>
        <script type="text/javascript" src="../js/jquery-1.8.3.js" >script>
        
        <script>
            $(function(){
                $("#select").click(function(){
                    //获取下面所有的 复选框并将其选中状态设置跟编码的前端 复选框保持一致。
                    //attr方法与JQ的版本有关,在1.8.3及以下有效。
                    //$("tbody input").attr("checked",this.checked);
                    $("tbody input").prop("checked",this.checked);
                });
            }); 
        script>

    head>
    <body>
        <table border="1" width="500" height="50" align="center" id="tbl" >
            <thead>
                <tr>
                    <td colspan="4">
                        <input type="button" value="添加" />
                        <input type="button" value="删除" />
                    td>
                tr>
                <tr>
                    <th><input type="checkbox" id="select">th>
                    <th>编号th>
                    <th>姓名th>
                    <th>年龄th>
                tr>
            thead>
            <tbody>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>1td>
                    <td>张三td>
                    <td>22td>
                tr>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>2td>
                    <td>李四td>
                    <td>25td>
                tr>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>3td>
                    <td>王五td>
                    <td>27td>
                tr>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>4td>
                    <td>赵六td>
                    <td>29td>
                tr>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>5td>
                    <td>田七td>
                    <td>30td>
                tr>
                <tr >
                    <td><input type="checkbox" class="selectOne"/>td>
                    <td>6td>
                    <td>汾九td>
                    <td>20td>
                tr>
            tbody>
        table>
    body>
html>

运行结果:
前端基础系列(七)JQuery基础(上篇)_第3张图片

你可能感兴趣的:(前端开发系列,前端开发)