JQuery 和 Bootstrap

https://jquery.com/
css

1. JQuery  的基础语法html

$(select).action()

 

2. 查找标签jquery

  基本选择器数组

  class选择器:
app

$(".className")

  id选择器:
框架

$("#id")

  标签选择器:
ide

$("tagName")

  配合使用:函数

$("div.c1")

  全部元素选择器字体

$("*")

  组合选择器:动画

$("#id, .className, tagName")

  层级选择器:

  x和y能够为任意选择器

$("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(元素选择器)// 选取全部包含一个或多个标签在其内的标签(指的是从后代元素找)

  例子:

$("div:has(h1)")// 找到全部后代中有h1标签的div标签
$("div:has(.c1)")// 找到全部后代中有c1样式类的div标签
$("li:not(.c1)")// 找到全部不包含c1样式类的li标签
$("li:not(:has(a))")// 找到全部后代中不含a标签的li标签

  JQuery 自定义模态框

<!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 { width: 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

  例子:

$(":checkbox")

  表单对象属性:

:enabled :disabled :checked :selected

  例子:

  找到可用的input标签

<form>
  <input name="email" disabled="disabled" />
  <input name="id" />
</form> $("input:enabled")  // 找到可用的input标签

   找到被选中的option:

<select id="s1">
  <option value="beijing">北京市</option>
  <option value="shanghai">上海市</option>
  <option selected value="guangzhou">广州市</option>
  <option value="shenzhen">深圳市</option>
</select>
$(":selected")  // 找到全部被选中的option

  筛选器方法:

  下一个元素:

$("#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; width: 20%; height: 100%; background-color: rgb(47, 53, 61); } .right { width: 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("color","red")//DOM操做:tag.style.color="red"
  示例
$("p").css("color", "red"); //将全部p标签的字体设置为红色

  位置操做

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

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

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

<!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> .c1 { width: 100px; height: 200px; background-color: red; } .c2 { height: 50px; width: 50px; position: fixed; bottom: 15px; right: 15px; background-color: #2b669a; } .hide { display: none; } .c3 { height: 100px; } </style>
</head>
<body>
<button id="b1" class="btn btn-default">点我</button>
<div class="c1"></div>
<div class="c3">1</div>
<div class="c3">2</div>
<div class="c3">3</div>
<div class="c3">4</div>
<div class="c3">5</div>
<div class="c3">6</div>
<div class="c3">7</div>
<div class="c3">8</div>
<div class="c3">9</div>
<div class="c3">10</div>
<div class="c3">11</div>
<div class="c3">12</div>
<div class="c3">13</div>
<div class="c3">14</div>
<div class="c3">15</div>
<div class="c3">16</div>
<div class="c3">17</div>
<div class="c3">18</div>
<div class="c3">19</div>
<div class="c3">20</div>
<div class="c3">21</div>
<div class="c3">22</div>
<div class="c3">23</div>
<div class="c3">24</div>
<div class="c3">25</div>
<div class="c3">26</div>
<div class="c3">27</div>
<div class="c3">28</div>
<div class="c3">29</div>
<div class="c3">30</div>
<div class="c3">31</div>
<div class="c3">32</div>
<div class="c3">33</div>
<div class="c3">34</div>
<div class="c3">35</div>
<div class="c3">36</div>
<div class="c3">37</div>
<div class="c3">38</div>
<div class="c3">39</div>
<div class="c3">40</div>
<div class="c3">41</div>
<div class="c3">42</div>
<div class="c3">43</div>
<div class="c3">44</div>
<div class="c3">45</div>
<div class="c3">46</div>
<div class="c3">47</div>
<div class="c3">48</div>
<div class="c3">49</div>
<div class="c3">50</div>


<button id="b2" class="btn btn-default c2 hide">返回顶部</button>
<script src="jquery-3.2.1.min.js"></script>
<script> $("#b1").on("click", function () { $(".c1").offset({left: 200, top:200}); }); $(window).scroll(function () { if ($(window).scrollTop() > 100) { $("#b2").removeClass("hide"); }else { $("#b2").addClass("hide"); } }); $("#b2").on("click", function () { $(window).scrollTop(0); }) </script>
</body>
</html>

  尺寸:

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

  文本操做

  HTML代码:

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

  文本值:

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

  值:

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

  列:

<input type="checkbox" value="basketball" name="hobby">篮球 <input type="checkbox" value="football" name="hobby">足球 <select multiple id="s1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

  设置值:

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

  获取被选中的checkbox或radio的值:

<label for="c1"></label>
<input name="gender" id="c1" type="radio" value="0">
<label for="c2"></label>
<input name="gender" id="c2" type="radio" value="1">

  可使用:

$("input[name='gender']:checked").val()

  自定义登陆检验:

<!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 action="">
  <div>
    <label for="input-name">用户名</label>
    <input type="text" id="input-name" name="name">
    <span class="error"></span>
  </div>
  <div>
    <label for="input-password">密码</label>
    <input type="password" id="input-password" name="password">
    <span class="error"></span>
  </div>
  <div>
    <input type="button" id="btn" value="提交">
  </div>
</form>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script> $("#btn").click(function () { var username = $("#input-name").val(); var password = $("#input-password").val(); if (username.length === 0) { $("#input-name").siblings(".error").text("用户名不能为空"); } if (password.length === 0) { $("#input-password").siblings(".error").text("密码不能为空"); } }) </script>
</body>
</html>

  属性操做

  用于ID等或自定义属性:

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

  用于checkbox和radio

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

  文档处理

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

$(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(){...})
 

  keydown和keyup事件组合示例:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title>Title</title>
</head>
<body>


<table border="1">
  <thead>
  <tr>
    <th>#</th>
    <th>姓名</th>
    <th>操做</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td><input type="checkbox"></td>
    <td>Egon</td>
    <td>
      <select>
        <option value="1">上线</option>
        <option value="2">下线</option>
        <option value="3">停职</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><input type="checkbox"></td>
    <td>Alex</td>
    <td>
      <select>
        <option value="1">上线</option>
        <option value="2">下线</option>
        <option value="3">停职</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><input type="checkbox"></td>
    <td>Yuan</td>
    <td>
      <select>
        <option value="1">上线</option>
        <option value="2">下线</option>
        <option value="3">停职</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><input type="checkbox"></td>
    <td>EvaJ</td>
    <td>
      <select>
        <option value="1">上线</option>
        <option value="2">下线</option>
        <option value="3">停职</option>
      </select>
    </td>
  </tr>
  <tr>
    <td><input type="checkbox"></td>
    <td>Gold</td>
    <td>
      <select>
        <option value="1">上线</option>
        <option value="2">下线</option>
        <option value="3">停职</option>
      </select>
    </td>
  </tr>
  </tbody>
</table>

<input type="button" id="b1" value="全选">
<input type="button" id="b2" value="取消">
<input type="button" id="b3" value="反选">


<script src="jquery-3.3.1.js"></script>
<script>

    var flag = false; // shift按键被按下的时候
 $(window).keydown(function (event) { console.log(event.keyCode); if (event.keyCode === 16){ flag = true; } }); // shift按键被抬起的时候
 $(window).keyup(function (event) { console.log(event.keyCode); if (event.keyCode === 16){ flag = false; } }); // select标签的值发生变化的时候
 $("select").change(function (event) { // 若是shift按键被按下,就进入批量编辑模式
        // shift按键对应的code是16
        // 判断当前select这一行是否被选中
 console.log($(this).parent().siblings().first().find(":checkbox")); var isChecked = $(this).parent().siblings().first().find(":checkbox").prop("checked"); console.log(isChecked); if (flag && isChecked) { // 进入批量编辑模式
            // 1. 取到当前select选中的值
            var value = $(this).val(); // 2. 给其余被选中行的select设置成和我同样的值
            // 2.1 找到那些被选中行的select
            var $select = $("input:checked").parent().parent().find("select") // 2.2 给选中的select赋值
 $select.val(value); } }); </script>
</body>
</html>

  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>

  实时监听input输入值变化示例:

<!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>实时监听input输入值变化</title>
</head>
<body>
<input type="text" id="i1">

<script src="jquery-3.2.1.min.js"></script>
<script>
  /* * oninput是HTML5的标准事件 * 可以检测textarea,input:text,input:password和input:search这几个元素的内容变化, * 在内容修改后当即被触发,不像onchange事件须要失去焦点才触发 * oninput事件在IE9如下版本不支持,须要使用IE特有的onpropertychange事件替代 * 使用jQuery库的话直接使用on同时绑定这两个事件便可。 * */ $("#i1").on("input propertychange", function () { alert($(this).val()); }) </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>

  阻止事件冒泡

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

  页面载入

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

  动画效果

// 基本 show([s,[e],[fn]]) hide([s,[e],[fn]]) toggle([s],[e],[fn]) // 滑动 slideDown([s],[e],[fn]) slideUp([s,[e],[fn]]) slideToggle([s],[e],[fn]) // 淡入淡出 fadeIn([s],[e],[fn]) fadeOut([s],[e],[fn]) fadeTo([[s],o,[e],[fn]]) fadeToggle([s,[e],[fn]]) // 自定义(了解便可) animate(p,[s],[e],[fn])

  自定义动画实例

<!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> div { position: relative; display: inline-block;
    } div>i { display: inline-block; color: red; position: absolute; right: -16px; top: -5px; opacity: 1;
    }
  </style>
</head>
<body>

<div id="d1">点赞</div>
<script src="jquery-3.2.1.min.js"></script>
<script> $("#d1").on("click", function () { var newI = document.createElement("i"); newI.innerText = "+1"; $(this).append(newI); $(this).children("i").animate({ opacity: 0 }, 1000) }) </script>
</body>
</html>

  each

  jQuery.each(collection, callback(indexInArray, valueOfElement)):

  描述:一个通用的迭代函数,它能够用来无缝迭代对象和数组。数组和相似数组的对象经过一个长度属性(如一个函数的参数对象)来迭代数字索引,从0到length - 1。其余对象经过其属性名进行迭代。

li =[10,20,30,40] $.each(li,function(i, v){ console.log(i, v);//index是索引,ele是每次循环的具体元素。 })

  输出  : 010     120     230     340  

  .each(function(index, Element)):

  描述:遍历一个jQuery对象,为每一个匹配元素执行一个函数。

 

  .each() 方法用来迭代jQuery对象中的每个DOM元素。每次回调函数执行时,会传递当前循环次数做为参数(从0开始计数)。因为回调函数是在当前DOM元素为上下文的语境中触发的,因此关键字 this 老是指向这个元素

// 为每个li标签添加foo $("li").each(function(){ $(this).addClass("c1"); });

  注意: jQuery的方法返回一个jQuery对象,遍历jQuery集合中的元素 - 被称为隐式迭代的过程。当这种状况发生时,它一般不须要显式地循环的 .each()方法:

也就是说,上面的例子没有必要使用each()方法,直接像下面这样写就能够了:

$("li").addClass("c1");  // 对全部标签作统一操做

  注意:

  在遍历过程当中可使用 return false提早结束each循环。

  终止each循环

return false;

 

  Bootstrap 前段框架

    https://v3.bootcss.com/

相关文章
相关标签/搜索