孙肖宁 发布的文章

描述:通过判断按的键盘上的那个按键,来控制颜色,并且把按键的编码显示出来。如果没有则提示没有。jQuery的版本是1.8.
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>按键变色</title>
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            margin: 100px auto 0;
        }

        .wrap h1 {
            text-align: center;
        }

        .wrap div {
            width: 400px;
            height: 300px;
            background: pink;
            font-size: 30px;
            text-align: center;
            line-height: 300px;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            var div = $("#bgChange");
            var span = $("#keyCodeSpan");

            $(document).keyup(function (e) {
                setColor(e.keyCode);
            });
            //接受按键并调用颜色和数值的函数
            function setColor(e) {
                switch (e) {
                    case 80:
                        setEle("pink", e);
                        break;
                    case 66:
                        setEle("blue", e);
                        break;
                    case 79:
                        setEle("orange", e);
                        break;
                    case 82:
                        setEle("red", e);
                        break;
                    case 89:
                        setEle("yellow", e);
                        break;
                    default :
                        alert("系统没有设置该颜色!");
                }
                //颜色和数值的函数
                function setEle(a, b) {
                    div.css("background-color", a);
                    span.text(b);
                }
            }
        });


    </script>
</head>
<body>
    <div class="wrap">
        <h1>按键改变颜色</h1>
        <div id="bgChange">
            keyCode为:
            <span id="keyCodeSpan">80</span>
        </div>
    </div>
</body>
</html>

描述:当页面拥有多个表单输入框的时候,点击回车自动切换到一行,并且下一行已经存在的文字处于选中状态。当鼠标经过某一行的时候,该行的文字处于选的状态。所使用的jquery的版本是1.8.
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回车换行</title>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            //点击回车之后,自动切换到下一行
            $("input").on("keyup",function (event) {
                if(event.keyCode === 13){
                    $(this).next().next().focus();
                }
            });
            //鼠标经过某一行的时候,该行所有的文字全部被选中
            $("input").on("mouseenter",function () {
                $(this).select();
            });
        })
    </script>
</head>
<body>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
    <input type="text" value="小宁博客"/><br>
</body>
</html>

jQuery的事件机制,指的是:jQuery对JavaScript操作DOM事件的封装,包括了:事件绑定、事件解绑、事件触发。

事件绑定

  • 简单事件绑定
    click(handler) 单击事件;
    blur(handler) 失去焦点事件;
    mouseenter(handler) 鼠标进入事件;
    mouseleave(handler) 鼠标离开事件;
    dbclick(handler) 双击事件;
    change(handler) 改变事件,如:文本框值改变,下来列表值改变等;
    focus(handler) 获得焦点事件;
    keydown(handler) 键盘按下事件;
    ......
  • bind方式(1.7版本以后的jQuery版本被on取代)
    作用:给匹配到的元素直接绑定事件。
    第一个参数:事件类型
    第二个参数:事件处理程序

    $("p").bind("click mouseenter", function(e){
      //事件响应方法
    });

    优缺点:

    • 可以同时绑定多个事件,比如:bind(“mouseenter mouseleave”, function(){})
    • 缺点:要绑定事件的元素必须存在文档中。
  • delegate方式(特点:性能高,支持动态创建的元素)
    作用:给匹配到的元素绑定事件,对支持动态创建的元素有效
    第一个参数:selector,要绑定事件的元素
    第二个参数:事件类型
    第三个参数:事件处理函数

    $(".parentBox").delegate("p", "click", function(){
      //为 .parentBox下面的所有的p标签绑定事件
    });

    优势:减少事件绑定次数提高效率,支持动态创建出来的元素绑定事件

  • on方式(最现代的方式,兼容zepto(移动端类似jQuery的一个库))
    jQuery1.7版本后,jQuery用on统一了所有的事件处理的方法
    作用:给匹配的元素绑定事件,包括了上面所有绑定事件方式的优点
    语法:
    第一个参数:events,绑定事件的名称可以是由空格分隔的多个事件(标准事件或者自定义事件)
    第二个参数:selector, 执行事件的后代元素
    第三个参数:data,传递给处理函数的数据,事件触发的时候通过event.data来使用
    第四个参数:handler,事件处理函数
    $(selector).on(events[,selector][,data],handler);
    表示给$(selector)绑定事件,当必须是它的内部元素span才能执行这个事件
    $(selector).on( "click",“span”, function() {});

    • 绑定多个事件

    表示给$(selector)匹配的元素绑定单击和鼠标进入事件
    $(selector).on(“click mouseenter”, function(){});

事件解绑

  • unbind() 方式
    作用:解绑 bind方式绑定的事件
    $(selector).unbind(); //解绑所有的事件
    $(selector).unbind(“click”); //解绑指定的事件
  • undelegate() 方式
    作用:解绑delegate方式绑定的事件
    $( selector ).undelegate(); //解绑所有的delegate事件
    $( selector).undelegate( “click” ); //解绑所有的click事件
  • off解绑on方式绑定的事件
    $(selector).off();// 解绑匹配元素的所有事件;
    $(selector).off("click");// 解绑匹配元素的所有click事件;
    $(selector).off( "click", "**" ); // 解绑所有代理的click事件,元素本身的事件不会被解绑。

事件触发

  • 简单事件触发
    $(selector).click(); //触发 click事件
  • trigger方法触发事件,触发浏览器行为
    $(selector).trigger(“click”);
  • triggerHandler触发 事件响应方法,不触发浏览器行为
    比如:文本框获得焦点的默认行为 $(selector).triggerHandler(“focus”);

jQuery事件对象介绍

  • event.data传递给事件处理程序的额外数据
  • event.currentTarget等同于this,当前DOM对象
  • event.pageX鼠标相对于文档左部边缘的位置
  • event.target 触发事件源,不一定===this
  • event.stopPropagation()阻止事件冒泡
  • event.preventDefault()阻止默认行为
  • event.type 事件类型:click,dbclick…
  • event.which 鼠标的按键类型:左1 中2 右3
  • event.keyCode 键盘按键代码

高度和宽度

  • 高度操作height() :
    作用:设置或获取匹配元素的高度值

    • 带参数表示设置高度$(selector).height(200);
    • 不带参数获取高度$(selector).height();
  • 宽度操作width() :
    作用:设置或获取匹配元素的宽度值

    • 带参数表示设置宽度
    • 不带参数获取宽度

注意:

  • 获取到的值仅仅是盒子和高度或宽度,和内外边距边框无关。
  • css()获取高度(或宽度width)和height获取高度(或宽度width)的区别:

    • css()获取高度(或宽度width)的返回值是number类型,没有单位。
    • height获取高度(或宽度width)的返回值是string类型,有单位。

    坐标值的操作

  • offset()
    作用:获取或设置元素相对于文档的位置

    • 无参数表示获取,返回值为:{left:num, top:num},值是相对于document的位置$(selector).offset();
    • 有参数表示设置,参数推荐使用数值类型$(selector).offset({left:100, top: 150});

      注意:距离页面最顶端或者最左侧的距离和有没有定位没有关系。设置offset后,如果元素没有定位(默认值:static),则被修改为relative。

  • position()
    作用:获取相对于其最近的具有定位的父元素的位置。距离父系盒子中带有定位的盒子的距离(获取的就是定位值,和margin/padding无关)

    • 获取,返回值为对象:{left:num, top:num}。$(selector).position();

    注意:只能获取,不能设置。

  • scrollTop()
    作用:获取或者设置元素垂直方向滚动的位置

    • 无参数表示获取偏移
    • 有参数表示设置偏移,参数为数值类型

    案例:$(selector).scrollTop(100);

  • scrollLeft()
    作用:获取或者设置元素水平方向滚动的位置
    案例:$(selector).scrollLeft(100);
    描述:

    • 垂直滚动条位置 是可滚动区域 在 可视区域上方的 被隐藏区域的高度。
    • 如果滚动条在最上方没有滚动 或者 当前元素没有出现滚动条,那么这个距离为0

描述:用户输入什么内容,点击按钮后,上面就动态显示什么内容。jquery版本1.8.
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态输入案例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        @keyframes blink {
            0%,
            100% {
                opacity: 1
            }
            50% {
                opacity: 0
            }
        }

        @-webkit-keyframes blink {
            0%,
            100% {
                opacity: 1
            }
            50% {
                opacity: 0
            }
        }

        @-moz-keyframes blink {
            0%,
            100% {
                opacity: 1
            }
            50% {
                opacity: 0
            }
        }

        .wrap {
            width: 1000px;
            text-align: center;
            margin: 100px auto 0;
        }

        .wrap h1 {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-weight: 300;
        }

        .word {
            font-weight: 700;
        }

        .typed-cursor {
            opacity: 1;
            -webkit-animation: blink .7s infinite;
            -moz-animation: blink .7s infinite;
            animation: blink .7s infinite;
            display: none;
        }

        .saySection {
            margin-top: 50px;
        }

        .saySection input {
            font-size: 30px;
        }

        .saySection .txtSay {
            padding-left: 10px;
        }

        .saySection .btnSay {
            display: inline-block;
            padding: 0 20px;
            cursor: pointer;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            //动态展示预留内容
            var str = "小宁博客";
            var arr = str.split("");
            var str2 = "";
            var num = 0;
            var timer = null;
            $(".typed-cursor").show();
            timer = setInterval(function () {
                if(arr[num]===undefined){
                    clearInterval(timer);
                    $(".typed-cursor").hide();
                }else {
                    str2 += arr[num];
                    $(".word").text(str2);
                    num++;
                }
            },200);
            //动态展示输入的内容
            $("#btnSay").click(function () {
                str = $("#inValue").val();
                $("#inValue").val("");
                arr = str.split();
                str2 = "";
                num = 0;
                timer = setInterval(function () {
                    if(arr[num]===undefined){
                        clearInterval(timer);
                        $(".typed-cursor").hide();
                    }else {
                        str2+=arr[num];
                        $(".word").text(str2);
                        num++;
                    }
                },200);
            })
        });

    </script>
</head>
<body>
<div class="wrap">
    <h1>
        You want to say : <span class="word"></span><span class="typed-cursor">|</span> !
    </h1>

    <div class="saySection">
        <input type="text" id="inValue" class="txtSay"/>
        <input type="button" value="Say" id="btnSay" class="btnSay"/>
    </div>
</div>
</body>
</html>

val(): 获取标签中的value属性的值。带有参数是赋值(类比js中的value属性)。

text(): 获取双闭合标签中的文本值。(不识别标签)(类比innerText)。

html(): 获取双闭合标签中的文本值。(识别标签)(类比innerHTML)。

描述:当点击全选是,选中下面全部的复选框,当复选框全部选中时,点击返现则取消选择,当复选框有没有选中的项的时候,全选按钮取消全选。使用的jquery的版本是1.8.
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全选和反选 </title>
    <style>
        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 300px;
        }
        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        tbody tr {
            background-color: #f0f0f0;
        }

        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            $("#theadInp").click(function () {
                $("#tbody input:checkbox").prop("checked",$(this).prop("checked"));
            });
            $("#tbody input:checkbox").click(function () {
                if($("#tbody input:checkbox").length === $("#tbody input:checked").length){
                    $("#theadInp").prop("checked",true);
                }else {
                    $("#theadInp").prop("checked",false);
                }
            })
        });
    </script>
</head>
<body>
<div>
    <table>
        <thead>
        <tr>
            <th><input type="checkbox" id="theadInp"></th>
            <td>标题</td>
        </tr>
        </thead>
        <tbody id="tbody">
        <tr>
            <td><input type="checkbox"></td>
            <td>1</td>
        </tr>
        <td><input type="checkbox"></td>
        <td>1</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>1</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>1</td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

描述:当用户点击添加数据的时候,弹出一个模态框,其他位置不可点击,点击关闭按钮的时候,关闭模态框。当输入姓名或专业之后,点击添加,就会在表格里面添加一行数据,如果内容为空就不可以添加数据,就会弹出输入内容不能为空。当点击删除按钮的时候,则会删除当前行。jquery版本是1.8。
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态添加和删除表格数据</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 410px;
            margin: 100px auto 0;
        }

        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
        }

        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        td a.get {
            text-decoration: none;
        }

        a.del:hover {
            text-decoration: underline;
        }

        tbody tr {
            background-color: #f0f0f0;
        }

        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }

        .btnAdd {
            width: 110px;
            height: 30px;
            font-size: 20px;
            font-weight: bold;
        }

        .form-item {
            height: 100%;
            position: relative;
            padding-left: 100px;
            padding-right: 20px;
            margin-bottom: 34px;
            line-height: 36px;
        }

        .form-item > .lb {
            position: absolute;
            left: 0;
            top: 0;
            display: block;
            width: 100px;
            text-align: right;
        }

        .form-item > .txt {
            width: 300px;
            height: 32px;
        }

        .mask {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            background: #000;
            opacity: 0.5;
            display: none;
        }

        .form-add {
            position: fixed;
            top: 30%;
            left: 50%;
            margin-left: -197px;
            padding-bottom: 20px;
            background: #fff;
            display: none;
        }

        .form-add-title {
            background-color: #f7f7f7;
            border-width: 1px 1px 0 1px;
            border-bottom: 0;
            margin-bottom: 15px;
            position: relative;
        }

        .form-add-title span {
            width: auto;
            height: 18px;
            font-size: 16px;
            font-family: 宋体;
            font-weight: bold;
            color: rgb(102, 102, 102);
            text-indent: 12px;
            padding: 8px 0px 10px;
            margin-right: 10px;
            display: block;
            overflow: hidden;
            text-align: left;
        }

        .form-add-title div {
            width: 16px;
            height: 20px;
            position: absolute;
            right: 10px;
            top: 6px;
            font-size: 30px;
            line-height: 16px;
            cursor: pointer;
        }

        .form-submit {
            text-align: center;
        }

        .form-submit input {
            width: 170px;
            height: 32px;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script>
        $(function () {
            //点击按钮显示遮罩层和添加数据表格
            $("#j_btnAddData").click(function () {
                $("#j_mask").show();
                $("#j_formAdd").show();
            });
            //点击里面的关闭按钮隐藏遮罩层和添加数据表格
            $("#j_hideFormAdd").click(function () {
                $("#j_mask").hide();
                $("#j_formAdd").hide();
            });
            //点击删除所在的标签,删除所在的tr
            $("a.get").click(function () {
                $(this).parent("td").parent("tr").remove();
            });
            //点击里面的添加内容,全部能容这个成tr嵌套td的形式添加到tbody中
            //点击里面的添加内容,全部能容这个成tr嵌套td的形式添加到tbody中
            $("#j_btnAdd").click(function () {
                if($("#j_txtLesson").val===""){
                    alert("内容不能为空");
                    return;
                }
                var tr = $("<tr></tr>");
                tr.html('<td>'+$("#j_txtLesson").val()+'</td><td>'+$("#j_txtBelSch").val()+'</td><td><a href="javascrip:;" class="get">删除</a></td>');
                $("#j_tb").append(tr);
                //新产生的tr没有时间绑定
                tr.find("a").click(function () {
                    tr.remove();
                });
                $("#j_mask").hide();
                $("#j_formAdd").hide();
                $("#j_txtLesson").val("");
            });

        })

    </script>
</head>
<body>
<div class="wrap">
    <div>
        <input type="button" value="添加数据" id="j_btnAddData" class="btnAdd"/>
    </div>
    <table>
        <thead>
        <tr>
            <th>姓名</th>
            <th>专业</th>
            <th>删除</th>
        </tr>
        </thead>
        <tbody id="j_tb">
        <tr>
            <td>孙肖宁</td>
            <td>软件工程</td>
            <td><a href="javascrip:;" class="get">删除</a></td>
        </tr>
        <tr>
            <td>css</td>
            <td>小宁博客</td>
            <td><a href="javascrip:;" class="get">删除</a></td>
        </tr>
        <tr>
            <td>小宁</td>
            <td>软件工程</td>
            <td><a href="javascrip:;" class="get">删除</a></td>
        </tr>
        <tr>
            <td>小宁论坛</td>
            <td>软件工程</td>
            <td><a href="javascrip:;" class="get">删除</a></td>
        </tr>
        </tbody>
    </table>
</div>
<div id="j_mask" class="mask"></div>
<div id="j_formAdd" class="form-add">
    <div class="form-add-title">
        <span>添加数据</span>
        <div id="j_hideFormAdd">x</div>
    </div>
    <div class="form-item">
        <label class="lb" for="j_txtLesson">姓名:</label>
        <input class="txt" type="text" id="j_txtLesson" placeholder="请输入姓名">
    </div>
    <div class="form-item">
        <label class="lb" for="j_txtBelSch">专业:</label>
        <input class="txt" type="text" id="j_txtBelSch" value="软件工程">
    </div>
    <div class="form-submit">
        <input type="button" value="添加" id="j_btnAdd">
    </div>
</div>
</body>
</html>

描述:当用户访问你的网页的时候,右下角缓慢弹出一个广告,并且随着页面的滑动与改变,时钟在同一位置。当鼠标点击关闭的时候,广告缓慢隐藏。(关闭按钮小编采用的是一张图片,广告也是一张图片)jquery的版本是1.8.
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>屏幕右下角的广告的弹出和关闭</title>
    <style type="text/css">
        .ad {
            position: fixed;
            right: 0;
            bottom: 0;
            width: 230px;
            height: 120px;
            background-image: url(images/ad.jpg);
            display: none;
        }

        .ad span {
            position: absolute;
            right: 0;
            top: 0;
            width: 40px;
            height: 18px;
            background-image: url(images/h.jpg);
            cursor: pointer;
        }
    </style>
    <script src="../jquery.min.js"></script>
    <script>
        $(function () {
            $(".ad").slideUp(1000).fadeIn(1000).children("span").click(function () {
                $(this).parent().fadeOut(1000);
            })
        })
    </script>
</head>
<body>
<div class="ani">屏幕右下角的广告的弹出和关闭</div>
<div class="ad">
    <span></span>
</div>
</body>
</html>

描述:鼠标放在一级导航的时候。二级导航缓慢显示。鼠标离开之后,二级导航缓慢隐藏。鼠标经过的时候,如果二级菜单还没有完全打开或者隐藏,则或停止当前动画。使用的jQuery的版本是1.8的。
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>缓慢下拉菜单</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        .wrap {
            width: 330px;
            height: 30px;
            margin: 100px auto 0;
            padding-left: 10px;
            background-image: url(images/bg.jpg);
        }

        .wrap li{
            background-image: url(images/libg.jpg);
        }

        .wrap > ul > li {
            float: left;
            margin-right: 10px;
            position: relative;
        }

        .wrap a {
            display: block;
            height: 30px;
            width: 100px;
            text-decoration: none;
            color: #000;
            line-height: 30px;
            text-align: center;
        }

        .wrap li ul {
            position: absolute;
            top: 30px;
            display: none;
        }
    </style>
    <script src="../jquery.min.js"></script>
    <script>
        $(window).ready(function () {
            var jqli = $(".wrap>ul>li");
            jqli.mouseenter(function () {
                $(this).children("ul").stop().slideDown(200);
            });
            jqli.mouseleave(function () {
                $(this).children("ul").stop().slideUp(200);
            });
        })
    </script>
</head>
<body>
<div class="wrap">
    <ul>
        <li>
            <a href="javascript:void(0);">一级菜单1</a>
            <ul>
                <li><a href="javascript:void(0);">二级菜单1</a></li>
                <li><a href="javascript:void(0);">二级菜单2</a></li>
                <li><a href="javascript:void(0);">二级菜单3</a></li>
            </ul>
        </li>
        <li>
            <a href="javascript:void(0);">一级菜单1</a>
            <ul>
                <li><a href="javascript:void(0);">二级菜单1</a></li>
                <li><a href="javascript:void(0);">二级菜单2</a></li>
                <li><a href="javascript:void(0);">二级菜单3</a></li>
            </ul>
        </li>
        <li>
            <a href="javascript:void(0);">一级菜单1</a>
            <ul>
                <li><a href="javascript:void(0);">二级菜单1</a></li>
                <li><a href="javascript:void(0);">二级菜单2</a></li>
                <li><a href="javascript:void(0);">二级菜单3</a></li>
            </ul>
        </li>
    </ul>
</div>
</body>
</html>