信息发布→ 登录 注册 退出

jQuery的事件处理你知道多少

发布时间:2026-01-11

点击量:
目录
  • 一、jQuery的事件处理
    • 1、页面载入事件
    • 2、事件绑定(bind)
    • 3、反绑定事件(unbind)
    • 4、一次*件绑定(one)
    • 5、模拟鼠标悬停(hover)
  • 总结

    一、jQuery的事件处理

    1、页面载入事件

    $(document).ready() --- onload

    2、事件绑定(bind)

    bind(type,[data],fn)

    type:表示事件类型(clickmouseovermouseout...)

    [data]:可选参数,表示传递给事件对象的额外数据

    fn:是一个函数(事件处理函数),当事件发生时执行的程序

    为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数

    <!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.0">
        <title>Document</title>
        <script src="../jq/jquery.js"></script>
    </head>
    <body>
        <button id="btn">确定</button>
        <script>
            $(function(){
                $('#btn').bind('click',function(){//可以给按钮绑定其他事件
                    alert('事件绑定')
                })
            })
        </script>
    </body>
    </html>

     显示效果:点击确定按钮之后,出现弹窗

    <!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.0">
        <title>Document</title>
        <script src="../jq/jquery.js"></script>
    </head>
    <body>
        <img src="../img/1.jpg" alt="" width="150" height="200">
        <script>
            $(function(){
                //通过鼠标的悬停、离开事件来改变img的图像
                $('img').bind('mouseover',function(){
                    $(this).attr({src:'../img/2.jpg'})//this表示的是img这个元素
                })
                $('img').bind('mouseout',function(){
                    $(this).attr({src:'../img/1.jpg'})
                })
            })
        </script>
    </body>
    </html>

     显示效果:当鼠标悬停在图片上时,显示的是一个图片。当鼠标离开这个图片时,显示的是另一张图片。反复交替,没有限制。

    3、反绑定事件(unbind)

    unbind([type],[data]):删除绑定的事件

    (1)不带参数:删除元素上绑定的所有事件

    (2)带参数:[type]表示事件类型

    <!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.0">
        <title>Document</title>
        <script src="../jq/jquery.js"></script>
    </head>
    <body>
        <img src="../img/1.jpg" alt="" width="150" height="200">
        <script>
            $(function(){
                //通过鼠标的悬停、离开事件来改变img的图像
                $('img').bind('mouseover',function(){
                    $(this).attr({src:'../img/2.jpg'})//this表示的是img这个元素
                })
                $('img').bind('mouseout',function(){
                    $(this).attr({src:'../img/1.jpg'})
                })
                $('img').unbind('mouseout')//解绑
            })
        </script>
    </body>
    </html>

     显示效果:鼠标离开图片之后,图片不会变成1.jpg

    4、一次*件绑定(one)

    绑定的事件只能执行一次

    <!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.0">
        <title>Document</title>
        <script src="../jq/jquery.js"></script>
    </head>
    <body>
        <img src="../img/1.jpg" alt="" width="150" height="200">
        <script>
            $(function(){
                //通过鼠标的悬停、离开事件来改变img的图像
                $('img').bind('mouseover',function(){
                    $(this).attr({src:'../img/2.jpg'})//this表示的是img这个元素
                })
                //一次*件绑定
                $('img').one('mouseout',function(){
                    $(this).attr({src:'../img/1.jpg'})
                })
            })
        </script>
    </body>
    </html>

    显示效果:鼠标离开图片后,图片会变成1.jpg,但是这种变化只会执行一次。第二次离开图片时,就不会变成1.jpg。

    5、模拟鼠标悬停(hover)

    <!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.0">    <title>Document</title>    <script src="../jq/jquery.js"></script></head><body>    <div style="width: 200px; height: 200px; background-color: red;"></div>    <script>        $(function(){            $('div').hover(function(){                $(this).css('backgroundColor','pink')            })        })    </script></body></html><!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.0">
        <title>Document</title>
        <script src="../jq/jquery.js"></script>
    </head>
    <body>
        <div style="width: 200px; height: 200px; background-color: red;"></div>
        <script>
            $(function(){
                $('div').hover(function(){
                    $(this).css('backgroundColor','pink')
                })
            })
        </script>
    </body>
    </html>

    显示效果:鼠标悬停在图片上时,图片由红色变为粉色。离开图片时并不会变回原来的红色。

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!   

    在线客服
    服务热线

    服务热线

    4008888355

    微信咨询
    二维码
    返回顶部
    ×二维码

    截屏,微信识别二维码

    打开微信

    微信号已复制,请打开微信添加咨询详情!