jQuery

1. 今日概要

jQuery是JavaScript的第三方模块(第三方类库)

  • 基于jQuery,自己开发一个功能
  • 现成的工具 依赖jQuery,例如:BootStrap动态效果

2. 快速上手

  • 下载jQuery
  • 应用jQuery
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>

    </head>
    <body>

        <h1 id="txt">中国联通</h1>

        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script type="text/javascript">
            // 利用jQuery中的功能实现某些效果
            $("#txt").text("广东移动");

            document.getElementById('txt').innerText = "广西移动";
        </script>
    </body>
</html>

2.1 选择器(直接寻找)

  • ID选择器
$("#ID")
  • 样式选择器(类选择器 class)
$(".c1")
  • 标签选择器
$("h1/div/table")
  • 层级选择器
$(".c1 .c2 a")
  • 多选择器
$("#c3,#c2,li")
  • 属性选择器
$(["input[name='n1']"])

2.2 选择器(间接寻找)

  • 找到上一个同级标签
$("#c1").prev()
  • 找到下第二个同级标签
$("#c1").next().next()
  • 找到所有的其他同级标签
$("#c1").siblings()
  • 找父级子级标签
$("#c1").parent()  //父级
$("#c1").parent().parent()  //父级的父级

$("#c1").children()  //所有的子级
$("#c1").children(".p10")  //所有的子级中寻找class=p10的标签

$("#c1").find(".p10")  //所有的子孙级中寻找class=p10的标签

案例:菜单的切换

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .menus{
                width: 200px;
                height: 800px;
                border: 1px solid red;
            }
            .menus .header{
                background-color: gold;
                padding: 10px 5px;
                border-bottom: 1px dotted #dddddd;
                cursor: pointer;
            }
            .menus .content a{
                display: block;
                padding: 5px 5px;
                border-bottom: 1px dotted #dddddd;
            }
            .hide{
                display: none;
            }
        </style>
    </head>
    <body>
        <div class="menus">
            <div class="item">
                <div class="header" onclick="clickMe(this);">上海</div>
                <div class="content hide">
                    <a>宝山区</a>
                    <a>青浦区</a>
                    <a>浦东新区</a>
                    <a>普陀区</a>
                </div>
            </div>

            <div class="item">
                <div class="header" onclick="clickMe(this);">北京</div>
                <div class="content hide">
                    <a>海淀区</a>
                    <a>朝阳区</a>
                    <a>大兴区</a>
                    <a>昌平区</a>
                </div>
            </div>
        </div>
        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script>
            function clickMe(self){
                var hasHide = $(self).next().hasClass("hide");
                if(hasHide){
                    $(self).next().removeClass("hide");
                }else{
                    $(self).next().addClass("hide");
                }
            }
        </script>
    </body>
</html>
<script>
    function clickMe(self){
        $(self).next().removeClass("hide");
        $(self).parent().siblings().find(".content").addClass("hide");
    }
</script>

2.3 操作样式

  • addClass
  • removeClass
  • hasClass

2.4 值的操作

<div id='c1'>内容</div>

$("#c1").text()                    //获取文本内容
$("#c1").text("休息")                //设置文本内容
<input type='text' id='c2' />
    
$("#c2").val()                    //获取用户输入的值
$("#c2").val("哈哈哈")                //设置值

案例:动态创建数据

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

        </style>
    </head>
    <body>
        <input type="text" id="txtUser" placeholder="用户名">
        <input type="text" id="txtEmail" placeholder="邮箱">
        <input type="button" value="提交" onclick="getInfo()">

        <ul id="view">
        </ul>

        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script>
            function getInfo(){
                // 1.获取用户输入的用户名和密码
                var username = $("#txtUser").val();
                var email = $("#txtEmail").val();
                var dataString = username + "-" + email;
                // 2.创建li标签,并在li内部写入内容
                var newLi = $("<li>").text(dataString);
                // 3.把新创建的li标签添加到ul里面
                $("#view").append(newLi);
            }
        </script>
    </body>
</html>

2.5 事件

<ul>
    <li></li>
    <li></li>
    <li></li>
</ul>

<script>
    $("li").click(function (){
        // 当点击li标签时,会自动执行这个函数
        // $(this) 当前点击的是那个标签
    })
</script>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>

        </style>
    </head>
    <body>
        <div>
            <ul>
                <li>百度</li>
                <li>谷歌</li>
                <li>搜狗</li>
            </ul>
        </div>

        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script>
            $("li").click(function(){
                var text = $(this).text();
                console.log(text);
            })
        </script>
    </body>
</html>
  • 在jQuery中可以在页面上删除某个标签
<div id="c1">内容</div>

%("#c1").remove();
  • 当页面框架加载完成之后执行的代码:
$(function () {
    // 当页面的框架加载完成之后,自动就执行(不需要元素内容加载完按成)
})

案例:表格操作

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

        </style>
    </head>
    <body>
        <table border="1px">
            <thead>
                <th>ID</th>
                <th>姓名</th>
                <th>操作</th>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>徐泽林</td>
                    <td>
                        <input type="button" value="删除" class="delete">
                    </td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>徐泽林</td>
                    <td>
                        <input type="button" value="删除" class="delete">
                    </td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>徐泽林</td>
                    <td>
                        <input type="button" value="删除" class="delete">
                    </td>
                </tr>
            </tbody>
        </table>


        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script>
            $(function (){
                // 找到所有class=delete的标签,绑定事件
                $(".delete").click(function(){
                    // 删除当前行的数据
                    $(this).parent().parent().remove();
                });
            })
        </script>
    </body>
</html>

3. 前端整合

  • HTML
  • CSS
  • JavaScript、jQuery
  • BootStrap(动态效果依赖jQuery)
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/plugins/bootstrap-3.4.1-dist/css/bootstrap.css">
        <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css">
        <style>
            .navbar{
                border-radius: 0;
            }
        </style>
    </head>
    <body>
        <div class="navbar navbar-inverse">
            <div class="container-fluid">
              <!-- Brand and toggle get grouped for better mobile display -->
              <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-9" aria-expanded="false">
                  <span class="sr-only">Toggle navigation</span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">网站LOGO</a>
              </div>
      
              <!-- Collect the nav links, forms, and other content for toggling -->
              <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-9">
                <ul class="nav navbar-nav">
                  <li class="active"><a href="#">主页</a></li>
                  <li><a href="#">商城</a></li>
                  <li><a href="#">售后中心</a></li>
                  <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                      <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>
                  </li>
                </ul>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">登录</a></li>
                    <li><a href="#">注册</a></li>
                    <li class="dropdown">
                      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">徐泽林 <span class="caret"></span></a>
                      <ul class="dropdown-menu">
                        <li><a href="#">个人资料</a></li>
                        <li><a href="#">我的账户</a></li>
                        <li><a href="#">修改密码</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">注销</a></li>
                      </ul>
                    </li>
                  </ul>
              </div><!-- /.navbar-collapse -->
            </div><!-- /.container-fluid -->
        </div>


        <div>
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                Launch demo modal
            </button>
            
            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                    </div>
                    <div class="modal-body">
                    ...
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
                </div>
            </div>
        </div>

        <div>
            <button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>

            <button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Tooltip on top">Tooltip on top</button>

            <button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</button>

            <button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="right" title="Tooltip on right">Tooltip on right</button>
        </div>

        <div>
            <button id="element" type="button" class="btn btn-lg btn-danger" title="我是一个标题" data-content="我是内容XXXXXXXXXXXXXXXXXXXXX">点我弹出/隐藏弹出框</button>
        </div>

        <div style="width: 700px;">
            <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                <ol class="carousel-indicators">
                  <li data-target="#carousel-example-generic" data-slide-to="0" class=""></li>
                  <li data-target="#carousel-example-generic" data-slide-to="1" class="active"></li>
                  <li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
                </ol>
                <div class="carousel-inner" role="listbox">
                  <div class="item">
                    <img data-src="holder.js/900x500/auto/#777:#555/text:First slide" alt="First slide [900x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iOTAwIiBoZWlnaHQ9IjUwMCIgdmlld0JveD0iMCAwIDkwMCA1MDAiIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiPjwhLS0KU291cmNlIFVSTDogaG9sZGVyLmpzLzkwMHg1MDAvYXV0by8jNzc3OiM1NTUvdGV4dDpGaXJzdCBzbGlkZQpDcmVhdGVkIHdpdGggSG9sZGVyLmpzIDIuNi4wLgpMZWFybiBtb3JlIGF0IGh0dHA6Ly9ob2xkZXJqcy5jb20KKGMpIDIwMTItMjAxNSBJdmFuIE1hbG9waW5za3kgLSBodHRwOi8vaW1za3kuY28KLS0+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj48IVtDREFUQVsjaG9sZGVyXzE4YTliZjI2MWU4IHRleHQgeyBmaWxsOiM1NTU7Zm9udC13ZWlnaHQ6Ym9sZDtmb250LWZhbWlseTpBcmlhbCwgSGVsdmV0aWNhLCBPcGVuIFNhbnMsIHNhbnMtc2VyaWYsIG1vbm9zcGFjZTtmb250LXNpemU6NDVwdCB9IF1dPjwvc3R5bGU+PC9kZWZzPjxnIGlkPSJob2xkZXJfMThhOWJmMjYxZTgiPjxyZWN0IHdpZHRoPSI5MDAiIGhlaWdodD0iNTAwIiBmaWxsPSIjNzc3Ii8+PGc+PHRleHQgeD0iMzA4LjI4OTA2MjUiIHk9IjI3MC4xIj5GaXJzdCBzbGlkZTwvdGV4dD48L2c+PC9nPjwvc3ZnPg==" data-holder-rendered="true">
                  </div>
                  <div class="item active">
                    <img data-src="holder.js/900x500/auto/#666:#444/text:Second slide" alt="Second slide [900x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iOTAwIiBoZWlnaHQ9IjUwMCIgdmlld0JveD0iMCAwIDkwMCA1MDAiIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiPjwhLS0KU291cmNlIFVSTDogaG9sZGVyLmpzLzkwMHg1MDAvYXV0by8jNjY2OiM0NDQvdGV4dDpTZWNvbmQgc2xpZGUKQ3JlYXRlZCB3aXRoIEhvbGRlci5qcyAyLjYuMC4KTGVhcm4gbW9yZSBhdCBodHRwOi8vaG9sZGVyanMuY29tCihjKSAyMDEyLTIwMTUgSXZhbiBNYWxvcGluc2t5IC0gaHR0cDovL2ltc2t5LmNvCi0tPjxkZWZzPjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+PCFbQ0RBVEFbI2hvbGRlcl8xOGE5YmYyNTY4NSB0ZXh0IHsgZmlsbDojNDQ0O2ZvbnQtd2VpZ2h0OmJvbGQ7Zm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgT3BlbiBTYW5zLCBzYW5zLXNlcmlmLCBtb25vc3BhY2U7Zm9udC1zaXplOjQ1cHQgfSBdXT48L3N0eWxlPjwvZGVmcz48ZyBpZD0iaG9sZGVyXzE4YTliZjI1Njg1Ij48cmVjdCB3aWR0aD0iOTAwIiBoZWlnaHQ9IjUwMCIgZmlsbD0iIzY2NiIvPjxnPjx0ZXh0IHg9IjI2NC45NDUzMTI1IiB5PSIyNzAuMSI+U2Vjb25kIHNsaWRlPC90ZXh0PjwvZz48L2c+PC9zdmc+" data-holder-rendered="true">
                  </div>
                  <div class="item">
                    <img data-src="holder.js/900x500/auto/#555:#333/text:Third slide" alt="Third slide [900x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iOTAwIiBoZWlnaHQ9IjUwMCIgdmlld0JveD0iMCAwIDkwMCA1MDAiIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiPjwhLS0KU291cmNlIFVSTDogaG9sZGVyLmpzLzkwMHg1MDAvYXV0by8jNTU1OiMzMzMvdGV4dDpUaGlyZCBzbGlkZQpDcmVhdGVkIHdpdGggSG9sZGVyLmpzIDIuNi4wLgpMZWFybiBtb3JlIGF0IGh0dHA6Ly9ob2xkZXJqcy5jb20KKGMpIDIwMTItMjAxNSBJdmFuIE1hbG9waW5za3kgLSBodHRwOi8vaW1za3kuY28KLS0+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj48IVtDREFUQVsjaG9sZGVyXzE4YTliZjIyMTNmIHRleHQgeyBmaWxsOiMzMzM7Zm9udC13ZWlnaHQ6Ym9sZDtmb250LWZhbWlseTpBcmlhbCwgSGVsdmV0aWNhLCBPcGVuIFNhbnMsIHNhbnMtc2VyaWYsIG1vbm9zcGFjZTtmb250LXNpemU6NDVwdCB9IF1dPjwvc3R5bGU+PC9kZWZzPjxnIGlkPSJob2xkZXJfMThhOWJmMjIxM2YiPjxyZWN0IHdpZHRoPSI5MDAiIGhlaWdodD0iNTAwIiBmaWxsPSIjNTU1Ii8+PGc+PHRleHQgeD0iMjk4LjMxMjUiIHk9IjI3MC4xIj5UaGlyZCBzbGlkZTwvdGV4dD48L2c+PC9nPjwvc3ZnPg==" data-holder-rendered="true">
                  </div>
                </div>
                <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                  <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                  <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                  <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                  <span class="sr-only">Next</span>
                </a>
              </div>
        </div>

        <script src="/static/js/jquery-3.7.1.min.js"></script>
        <script src="/static/plugins/bootstrap-3.4.1-dist/js/bootstrap.js"></script>
        <script>
            $(function () {
                $('[data-toggle="tooltip"]').tooltip();
                $('#element').popover({trigger:"click", placement:"bottom"});
                $('.carousel').carousel({
                    interval: 2000
                });
            })
        </script>
    </body>
</html>

案例:添加数据页面

  • 人员信息录入功能,需要提供用户信息:
    • 用户名、年龄、薪资、部门、入职时间(*)
      • 对于入职时间,需要添加时间选择器(插件:datetimepicker)
  • 源码见【DjangoTest02】
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/plugins/bootstrap-3.4.1-dist/css/bootstrap.css">
        <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css">
        <link rel="stylesheet" href="/static/plugins/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.css">
        <style>

        </style>
    </head>
    <body>
      <div class="container">
        <form class="form-horizontal" style="margin-top: 20px;">
          <div class="row clearfix">
            <div class="col-xs-6">
              <div class="form-group">
                <label class="col-sm-2 control-label">姓名</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" placeholder="请输入姓名">
                </div>
              </div>
            </div>

            <div class="col-xs-6">
              <div class="form-group">
                <label class="col-sm-2 control-label">年龄</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" placeholder="请输入年龄">
                </div>
              </div>
            </div>
          </div>

          <div class="row clearfix">
            <div class="col-xs-6">
              <div class="form-group">
                <label class="col-sm-2 control-label">薪资</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" placeholder="请输入薪资">
                </div>
              </div>
            </div>
            <div class="col-xs-6">
              <div class="form-group">
                <label class="col-sm-2 control-label">部门</label>
                <div class="col-sm-10">
                  <!-- <input type="text" class="form-control" placeholder="薪资"> -->
                  <select class="form-control">
                    <option>IT部</option>
                    <option>人事部</option>
                    <option>产品部</option>
                    <option>销售部</option>
                    <option>运营部</option>
                  </select>
                </div>
              </div>
            </div>
          </div>

          <div class="row clearfix">
            <div class="col-xs-6">
              <div class="form-group">
                <label class="col-sm-2 control-label">入职日期</label>
                <div class="col-sm-10">
                  <input type="date" id="dt" class="form-control" placeholder="请输入入职时间">

                </div>
              </div>
            </div>
          </div>

          <div class="row clearfix">
            <div class="col-xs-6">
              <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">提交</button>
              </div>
            </div>
            </div>
          </div>
        </form>
      </div>



      <script src="/static/js/jquery-3.7.1.min.js"></script>
      <script src="/static/plugins/bootstrap-3.4.1-dist/js/bootstrap.js"></script>
      <script type="text/javascript">
        
      </script>
    </body>
</html>

4. 总结

  1. 了解HTML标签、标签结构;基于它可以实现简单的页面
  2. CSS,了解基本样式;在别人模板的基础上改
  3. JavaScript、jQuery,了解基本使用
    • 事件绑定/寻找标签/操作标签
    • 导入插件实现想要的页面效果
  • 后续开发过程中,对于前端就是在BootStrap的基础上整改

评论