• JQurey


    jQuery介绍

      jQuery是一个轻量级的、兼容多浏览器的JavaScript库。

    jQuery优势

      一款轻量级的JS框架。jQuery核心js文件才几十kb,不会影响页面加载速度。

    jQuery内容

    1.选择器   2.筛选器   3.样式操作   4. 文本操作   5.属性操作   6.文本处理   7.事件   8.动画效果   9.插件   10. each data  Ajax

    jQuery对象

      jQuery对象就是通过jQuery包装DOM对象后产生的对象。jQuery对象是 jQuery独有的。如果一个对象是 jQuery对象,那么它就可以使用jQuery里的方法:例如$(“#i1”).html()。

    $("#i1").html()的意思是:获取id值为 i1的元素的html代码。其中 html()是jQuery里的方法。

    相当于: document.getElementById("i1").innerHTML;

    var $variable = jQuery对像
    var variable = DOM对象
    $variable[0]//jQuery对象转成DOM对象

    jQuery基础语法

      $(selector).action()

    查找标签

    基本选择器

    id选择器    $("#id")

    标签选择器   $("tagName")

    class选择器     $(".className")

    配合使用      $("div.c1") // 找到有c1 class类的div标签

    所有元素选择器     $("*")

    组合选择器      $("#id, .className, tagName")

    层级选择器   

    $("x y");// x的所有后代y(子子孙孙)
    $("x > y");// x的所有儿子y(儿子)
    $("x + y")// 找到所有紧挨在x后面的y
    $("x ~ y")// x之后所有的兄弟y

    基本选择器

    :first // 第一个
    :last // 最后一个
    :eq(index)// 索引等于index的那个元素
    :even // 匹配所有索引值为偶数的元素,从 0 开始计数
    :odd // 匹配所有索引值为奇数的元素,从 0 开始计数
    :gt(index)// 匹配所有大于给定索引值的元素
    :lt(index)// 匹配所有小于给定索引值的元素
    :not(元素选择器)// 移除所有满足not条件的标签
    :has(元素选择器)// 选取所有包含一个或多个标签在其内的标签(指的是从后代元素找)

    自定义模态框

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>自定义模态框</title>
      <style>
        .cover {
          position: fixed;
          left: 0;
          right: 0;
          top: 0;
          bottom: 0;
          background-color: darkgrey;
          z-index: 999;
        }
        .modal {
           600px;
          height: 400px;
          background-color: white;
          position: fixed;
          left: 50%;
          top: 50%;
          margin-left: -300px;
          margin-top: -200px;
          z-index: 1000;
        }
        .hide {
          display: none;
        }
      </style>
    </head>
    <body>
    <input type="button" value="" id="i0">
    
    <div class="cover hide"></div>
    <div class="modal hide">
      <label for="i1">姓名</label>
      <input id="i1" type="text">
       <label for="i2">爱好</label>
      <input id="i2" type="text">
      <input type="button" id="i3" value="关闭">
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script>
      var tButton = $("#i0")[0];
      tButton.onclick=function () {
        var coverEle = $(".cover")[0];
        var modalEle = $(".modal")[0];
    
        $(coverEle).removeClass("hide");
        $(modalEle).removeClass("hide");
      };
    
      var cButton = $("#i3")[0];
      cButton.onclick=function () {
        var coverEle = $(".cover")[0];
        var modalEle = $(".modal")[0];
    
        $(coverEle).addClass("hide");
        $(modalEle).addClass("hide");
      }
    </script>
    </body>
    </html>

    属性选择器

    [attribute]
    [attribute=value]// 属性等于
    [attribute!=value]// 属性不等于
    // 示例
    <input type="text">
    <input type="password">
    <input type="checkbox">
    $("input[type='checkbox']");// 取到checkbox类型的input标签
    $("input[type!='text']");// 取到类型不是text的input标签

    表单筛选器

    :text
    :password
    :file
    
    :radio
    :checkbox
    
    :submit
    :reset
    :button

    表单对象属性

    :enabled
    :disabled
    :checked
    :selected

    筛选器方法

    下一个元素

    $("#id").next()
    $("#id").nextAll()
    $("#id").nextUntil("#i2")

    上一个元素

    $("#id").prev()
    $("#id").prevAll()
    $("#id").prevUntil("#i2")

    父亲元素

    $("#id").parent()
    $("#id").parents()  // 查找当前元素的所有的父辈元素
    $("#id").parentsUntil() // 查找当前元素的所有的父辈元素,直到遇到匹配的那个元素为止。

    儿子和兄弟元素:

    $("#id").children();// 儿子们
    $("#id").siblings();// 兄弟们

    查找

    搜索所有与指定表达式匹配的元素。这个函数是找出正在处理的元素的后代元素的好方法。

    $("div").find("p")

    等价于$("div p")

    筛选

    筛选出与指定表达式匹配的元素集合。这个方法用于缩小匹配的范围。用逗号分隔多个表达式。

    $("div").filter(".c1")  // 从结果集中过滤出有c1样式类的

    等价于 $("div.c1")

    .first() // 获取匹配的第一个元素
    .last() // 获取匹配的最后一个元素
    .not() // 从匹配元素的集合中删除与指定表达式匹配的元素
    .has() // 保留包含特定后代的元素,去掉那些不含有指定后代的元素。
    .eq() // 索引值等于指定值的元素

    左侧菜单

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>左侧菜单示例</title>
      <style>
        .left {
          position: fixed;
          left: 0;
          top: 0;
           20%;
          height: 100%;
          background-color: rgb(47, 53, 61);
        }
    
        .right {
           80%;
          height: 100%;
        }
    
        .menu {
          color: white;
        }
    
        .title {
          text-align: center;
          padding: 10px 15px;
          border-bottom: 1px solid #23282e;
        }
    
        .items {
          background-color: #181c20;
    
        }
        .item {
          padding: 5px 10px;
          border-bottom: 1px solid #23282e;
        }
    
        .hide {
          display: none;
        }
      </style>
    </head>
    <body>
    
    <div class="left">
      <div class="menu">
        <div class="item">
          <div class="title">菜单一</div>
          <div class="items">
            <div class="item">111</div>
            <div class="item">222</div>
            <div class="item">333</div>
        </div>
        </div>
        <div class="item">
          <div class="title">菜单二</div>
          <div class="items hide">
          <div class="item">111</div>
          <div class="item">222</div>
          <div class="item">333</div>
        </div>
        </div>
        <div class="item">
          <div class="title">菜单三</div>
          <div class="items hide">
          <div class="item">111</div>
          <div class="item">222</div>
          <div class="item">333</div>
        </div>
        </div>
      </div>
    </div>
    <div class="right"></div>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    
    <script>
      $(".title").click(function (){  // jQuery绑定事件
        // 隐藏所有class里有.items的标签
        // $(".items").addClass("hide");  //批量操作
        // $(this).next().removeClass("hide");
        
        // jQuery链式操作
        $(this).next().removeClass('hide').parent().siblings().find('.items').addClass('hide')
      });
    </script>

    操作标签

    样式操作

    addClass();// 添加指定的CSS类名。
    removeClass();// 移除指定的CSS类名。
    hasClass();// 判断样式存不存在
    toggleClass();// 切换CSS类名,如果有就移除,如果没有就添加。

    css

    css("color","red")//DOM操作:tag.style.color="red"

    位置操作

    offset()// 获取匹配元素在当前窗口的相对偏移或设置元素位置
    position()// 获取匹配元素相对父元素的偏移
    scrollTop()// 获取匹配元素相对滚动条顶部的偏移
    scrollLeft()// 获取匹配元素相对滚动条左侧的偏移

    .offset()方法允许我们检索一个元素相对于文档(document)的当前位置。

    .position()的差别在于: .position()是相对于相对于父级元素的位移。

    尺寸

    height()
    width()
    innerHeight()
    innerWidth()
    outerHeight()
    outerWidth()

    文本操作

    HTML代码:

    html()// 取得第一个匹配元素的html内容
    html(val)// 设置所有匹配元素的html内容

    文本值

    text()// 取得所有匹配元素的内容
    text(val)// 设置所有匹配元素的内容

    val()// 取得第一个匹配元素的当前值
    val(val)// 设置所有匹配元素的值
    val([val1, val2])// 设置多选的checkbox、多选select的值

    设置值

    $("[name='hobby']").val(['basketball', 'football']);
    $("#s1").val(["1", "2"])

    属性操作

    用于ID等或自定义属性:

    attr(attrName)// 返回第一个匹配元素的属性值
    attr(attrName, attrValue)// 为所有匹配元素设置一个属性值
    attr({k1: v1, k2:v2})// 为所有匹配元素设置多个属性值
    removeAttr()// 从每一个匹配的元素中删除一个属性

    用于checkbox和radio

    prop() // 获取属性
    removeProp() // 移除属性

    prop和attr区别

    attr全称attribute(属性) 

    prop全称property(属性)

    虽然都是属性,但他们所指的属性并不相同,attr所指的属性是HTML标签属性,而prop所指的是DOM对象属性,可以认为attr是显式的,而prop是隐式的。

    1. 对于标签上有的能看到的属性和自定义属性都用attr
    2. 对于返回布尔值的比如checkbox、radio和option的是否被选中都用prop。

    文档处理

    添加到指定元素内部的后面

    $(A).append(B)// 把B追加到A
    $(A).appendTo(B)// 把A追加到B

    添加到指定元素内部的前面

    $(A).prepend(B)// 把B前置到A
    $(A).prependTo(B)// 把A前置到B

    添加到指定元素外部的后面

    $(A).after(B)// 把B放到A的后面
    $(A).insertAfter(B)// 把A放到B的后面

    添加到指定元素外部的前面

    $(A).before(B)// 把B放到A的前面
    $(A).insertBefore(B)// 把A放到B的前面

    移除和清空元素

    remove()// 从DOM中删除所有匹配的元素。
    empty()// 删除匹配的元素集合中所有的子节点。

    替换

    replaceWith()
    replaceAll()

    克隆

    clone()

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>克隆</title>
      <style>
        #b1 {
          background-color: deeppink;
          padding: 5px;
          color: white;
          margin: 5px;
        }
        #b2 {
          background-color: dodgerblue;
          padding: 5px;
          color: white;
          margin: 5px;
        }
      </style>
    </head>
    <body>
    
    <button id="b1">屠龙宝刀,点击就送</button>
    <hr>
    <button id="b2">屠龙宝刀,点击就送</button>
    
    <script src="jquery-3.2.1.min.js"></script>
    <script>
      // clone方法不加参数true,克隆标签但不克隆标签带的事件
      $("#b1").on("click", function () {
        $(this).clone().insertAfter(this);
      });
      // clone方法加参数true,克隆标签并且克隆标签带的事件
      $("#b2").on("click", function () {
        $(this).clone(true).insertAfter(this);
      });
    </script>
    </body>
    </html>

    事件

    click(function(){...})
    hover(function(){...})
    blur(function(){...})
    focus(function(){...})
    change(function(){...})
    keyup(function(){...})

    hover事件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p>苍茫的天涯是我的哎,绵绵的青山脚下一片海!</p>
    
    <script src="jQuery-3.3.1.js">
    </script>
    <script>
        $('p').hover(
            function () {
                alert('来啦,老弟')
            },
            function () {
                alert('慢走哦~')
            }
        )
    </script>
    </body>
    </html>

    事件绑定

    .on(events[,selector],function(){})

    events:事件

    selector:选择器

    function:事件处理函数

    移除事件

    .off(events[,selector][,function(){}])

    off()方法移除用.on()绑定事件处理程序

    events:事件

    selector:选择器‘

    function:事件处理函数

    阻止后续事件执行

    return false; 

    e.preventDefault();

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>阻止默认事件</title>
    </head>
    <body>
    
    <form action="">
        <button id="b1">点我</button>
    </form>
    
    <script src="jquery-3.3.1.min.js"></script>
    <script>
        $("#b1").click(function (e) {
            alert(123);
            //return false;
            e.preventDefault();
        });
    </script>
    </body>
    </html>

    像click、keydown等DOM中定义的事件,我们都可以使用`.on()`方法来绑定事件,但是`hover`这种jQuery中定义的事件就不能用`.on()`方法来绑定了。

    想使用事件委托的方式绑定hover事件处理函数,可以参照如下代码分两步绑定事件:

    $('ul').on('mouseenter', 'li', function() {//绑定鼠标进入事件
        $(this).addClass('hover');
    });
    $('ul').on('mouseleave', 'li', function() {//绑定鼠标划出事件
        $(this).removeClass('hover');
    });

    阻止事件冒泡

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>阻止事件冒泡</title>
    </head>
    <body>
    <div>
        <p>
            <span>点我</span>
        </p>
    </div>
    <script src="jquery-3.3.1.min.js"></script>
    <script>
        $("span").click(function (e) {
            alert("span");
            e.stopPropagation();
        });
    
        $("p").click(function () {
            alert("p");
        });
        $("div").click(function () {
            alert("div");
        })
    </script>
    </body>
    </html>

    页面载入

    当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度。

    $(document).ready(function(){
    // 在这里写你的JS代码...
    })
    
    
    
    $(function(){
    // 你在这里写你的代码
    })

    文档加载完绑定事件,并且阻止默认事件发生

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>登录注册示例</title>
      <style>
        .error {
          color: red;
        }
      </style>
    </head>
    <body>
    
    <form id="myForm">
      <label for="name">姓名</label>
      <input type="text" id="name">
      <span class="error"></span>
      <label for="passwd">密码</label>
      <input type="password" id="passwd">
      <span class="error"></span>
      <input type="submit" id="modal-submit" value="登录">
    </form>
    
    <script src="jquery-3.2.1.min.js"></script>
    <script src="s7validate.js"></script>
    <script>
      function myValidation() {
        // 多次用到的jQuery对象存储到一个变量,避免重复查询文档树
        var $myForm = $("#myForm");
        $myForm.find(":submit").on("click", function () {
          // 定义一个标志位,记录表单填写是否正常
          var flag = true;
          $myForm.find(":text, :password").each(function () {
            var val = $(this).val();
            if (val.length <= 0 ){
              var labelName = $(this).prev("label").text();
              $(this).next("span").text(labelName + "不能为空");
              flag = false;
            }
          });
          // 表单填写有误就会返回false,阻止submit按钮默认的提交表单事件
          return flag;
        });
        // input输入框获取焦点后移除之前的错误提示信息
        $myForm.find("input[type!='submit']").on("focus", function () {
          $(this).next(".error").text("");
        })
      }
      // 文档树就绪后执行
      $(document).ready(function () {
        myValidation();
      });
    </script>
    </body>
    </html>

    与window.onload的区别

    window.onload()函数有覆盖现象,必须等待着图片资源加载完成之后才能调用

    jQuery的这个入口函数没有函数覆盖现象,文档加载完成之后就可以调用(建议使用此函数)

    事件委托

    事件委托是通过事件冒泡的原理,利用父标签去捕获子标签的事件

    dayehui
  • 相关阅读:
    湘志恒善.NET 企业实训新学员必读手册
    周末电脑城有感硬件和软件价格的升降(实物图9.22更新)
    企业I期做项目之前的小例子
    商学院企业I班暑期作业 【2008年8月12日更新】
    项目公司机房升级
    android小应用帮美女更衣系列一(附源码)
    android小应用帮美女更衣系列二(附源码)
    @synthesize 和 @property
    VS2008下载地址和版本破解
    Android腾讯微薄客户端开发十三:提及篇(与我有关的微博)
  • 原文地址:https://www.cnblogs.com/zrh-960906/p/11502621.html
Copyright © 2020-2023  润新知