jQuery(2)——jQuery鼠标事件

jQuery事件


什么是事件?

页面对不同访问者的响应叫做事件。
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。

在 jQuery 中,大多数 DOM 事件都有一个等效的 jQuery 方法。


jQuery常用的事件

$(document).ready() 文档就绪事件

这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。
如果在文档没有完全加载之前就运行函数,操作可能失败。

常用事件列表:

鼠标事件键盘事件表单事件文档/窗口事件
clickkeypresssubmitload
dblclickkeydownchangeresize
mouseenterkeyupfocussroll
mouseleaveblurunload

本文的主角:鼠标事件,以下是对鼠标事件的练习:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>jQuery事件学习</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script>
    $(document).ready(function(){
        //鼠标点击事件
        $(".left button").click(
            function(){
                $(".right").append("<li>click()执行了!</li>");
            }
        )
        $(".left span").dblclick(
            function(){
                $(".right").append("<li>dblclick()执行了!</li>");
            }
        )
        //鼠标移入/移出事件
        $(".mouse").mouseenter(
            function(){
                $(".right").append("<li style='color:red;'>mouseenter()执行了!</li>");
            }
        )
        $(".mouse").mouseleave(
            function(){
                $(".right").append("<li style='color:red;'>mouseleave()执行了!</li>");
            }
        )
        //还可以用hover()来合并两个方法
        $(".hover").hover(
            function(){ 
                $(".right").append("<li style='color:gray;'>hover()的mouseenter()执行了!</li>");
            }, 
            function(){ 
                $(".right").append("<li style='color:gray;'>hover()的mouseenter()执行了!</li>");
            }
        );

        //鼠标按下/鼠标松开事件
        $(".mouse").mousedown(
            function(){
                $(".right").append("<li style='color:blue;'>mousedown()执行了!</li>");
            }
        )
        $(".mouse").mouseup(
            function(){
                $(".right").append("<li style='color:blue;'>mouseup()执行了!</li>");
            }
        )
        //focus焦点事件
        $("#input").focus(
            function(){
                $("#input").css("background","yellow");
            }
        )
        //blur失焦事件
        $("#input").blur(
            function(){
                $("#input").css("background","rgb(184, 202, 133)");
            }
        )
        
    })
    </script>
    <style>
        .left,.right{
            width:49%;
            height:auto;
            float:left
            }
        .right{
            border: 2px solid #223333;
            font-size: 20px;
            color: green;
        }
        .left span{
            cursor:pointer;
            border: 1px solid yellow;
            background-color: #f19f34;
        }
        .mouse,.hover{
            width:200px;
            height: 200px;
            border: 2px solid green;
            background-color: #f19f34;
        }
        .hover{
            border: 2px solid rgb(204, 116, 33);
            background-color: #eff162; 
        }
        #input{
            width:300px;
            background-color: rgb(184, 202, 133);
            color: rgb(211, 210, 155);
        }
    </style>
</head>
<body>
    <div id="main">
        <div class="left">
            <button>点击我</button>
            <span>双击我</span>
            <br>
            <div class="mouse">鼠标移入/移出响应事件</div>
            <div class="hover">hover()方法</div>
            <br>
            <input id="input" type="text" placeholder="focus事件">
        </div>
        <div class="right">事件提示:</div>
    </div>
</body>
</html>

demo示例图:
events


在线预览:

See the Pen jQuery鼠标事件 by 练涛 (@liantao) on CodePen.

width="100%" height="300" scrolling="no" title="jQuery鼠标事件" src="//codepen.io/liantao/embed/jeOpdW/?height=300&theme-id=34712&default-tab=js,result&embed-version=2" allowfullscreen="true">See the Pen jQuery鼠标事件 by 练涛 ( @liantao) on CodePen.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人生莫如饥荒

如果对您有帮助,也可以给点赏钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值