插件窝 干货文章 深入了解jQuery常见事件

深入了解jQuery常见事件

class div span 事件 496    来源:    2024-10-12

jQuery是一种广泛应用于网页开发中的JavaScript库,它提供了许多强大的功能和方法,其中事件处理是它的一个重要特性。在网页开发过程中,我们经常需要通过触发和处理事件来实现交互效果和页面动态效果。本文将深入探讨常见的jQuery事件,并通过具体的代码示例来展示它们的用法。

1. 点击事件(click)

点击事件是最常见的事件之一,在用户点击某个元素时触发。通过jQuery,我们可以很方便地为特定元素添加点击事件,并在事件发生时执行相应的操作。下面是一个简单的点击事件的示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击事件示例</title>
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script>
$(document).ready(function(){
    $("#myButton").click(function(){
        alert("按钮被点击了!");
    });
});
</script>
</head>
<body>
<button id="myButton">点击我</button>
</body>
</html>

在这个例子中,当按钮被点击时,会弹出一个提示框显示“按钮被点击了!”。

2. 鼠标移入移出事件(mouseenter、mouseleave)

鼠标移入移出事件分别在鼠标进入和离开元素时触发。这两个事件通常用于实现悬浮效果或者菜单展开收起等功能。下面是一个鼠标移入移出事件的示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鼠标移入移出事件示例</title>
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script>
$(document).ready(function(){
    $("#myDiv").mouseenter(function(){
        $(this).css("background-color", "yellow");
    });
    
    $("#myDiv").mouseleave(function(){
        $(this).css("background-color", "white");
    });
});
</script>
</head>
<body>
<div id="myDiv" style="width: 100px; height: 100px;">鼠标移入移出我</div>
</body>
</html>

在这个例子中,当鼠标移入

元素时,背景色会变成黄色;当鼠标移出
元素时,背景色会恢复为白色。

3. 键盘事件(keydown、keypress、keyup)

键盘事件可以捕获用户在键盘上的操作,如按下、按住和释放按键等。下面是一个键盘事件的示例,分别演示了keydown、keypress和keyup事件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>键盘事件示例</title>
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script>
$(document).ready(function(){
    $(document).keydown(function(e){
        console.log("键码:" + e.keyCode);
    });
    
    $(document).keypress(function(e){
        console.log("按键:" + String.fromCharCode(e.which));
    });
    
    $(document).keyup(function(){
        console.log("按键释放");
    });
});
</script>
</head>
<body>
在此处点击键盘
</body>
</html>

在这个例子中,当在页面任意位置按下键盘时,会在控制台输出相应的键码或按键信息,以及按键释放的提示。

4. 双击事件(dblclick)

双击事件在用户双击某个元素时触发,常用于实现双击编辑或弹出详细信息等功能。下面是一个双击事件的示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双击事件示例</title>
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script>
$(document).ready(function(){
    $("#myText").dblclick(function(){
        $(this).css("font-weight", "bold");
    });
});
</script>
</head>
<body>
<p id="myText">双击我改变字体加粗</p>
</body>
</html>

在这个例子中,当段落元素被双击时,字体会变成加粗样式。

通过以上代码示例,我们初步了解了jQuery中常见事件的使用方法,包括点击事件、鼠标移入移出事件、键盘事件和双击事件。在实际开发中,我们可以根据具体需求使用这些事件来实现各种交互效果和动态效果,使页面更加生动和吸引人。希望本文对您有所帮助,欢迎继续深入学习和探索jQuery事件的更多用法。

以上就是深入了解jQuery常见事件的详细内容,更多请关注其它相关文章!

        <!-- <div class="ask_line-container" >
            <div class="ask_line"></div>
            <button type="button" class="ask_text test-iframe-handle">
            没有解决问题?点击使用智能助手
            </button>
            <div class="ask_line"></div>
        </div> -->
                    <div class="community flexRow newcommunity">
                <div class="comleft flexRow newcomlimg">

                        <img src="/uploads/picture/2024/12/202410101037564231_1332.png" class="comlimg newcomlimg" alt="最佳 Windows 性能的顶级免费优化软件">

                    <div class="comldiv flexColumn newcomldiv">
                        最佳 Windows 性能的顶级免费优化软件

                    </div>
                </div>

                    下载

            </div>
                    <div class="phpwzggBox" style="width: 960px; margin: 20px auto;">
        <script type="text/javascript" smua="d=p&amp;s=b&amp;u=u2839554&amp;w=300&amp;h=250" src="https://www.nkscdn.com/smu/o.js"></script>
        </div>
                    <div class="wzconlabels">
                            <div class="wzconBq" style="display: inline-flex;">
                <span>相关标签:</span>
                <div class="wzcbqd" style="display:flex;">
                    键盘事件 点击事件 JavaScript jquery 事件 键盘事件                    </div>
            </div>
                            <div style="display: inline-flex;float: right; color:#333333;">来源:</div>
                        </div>


        <div class="wzconFx">

                                        <img src="">
                    <span>收藏</span>



                                        <img src="">
                    <span>点赞</span>

        </div>



        <div class="wzconOtherwz">

                    <span>上一篇:为什么我们需要阻止事件冒泡</span>


                    <span>下一篇:前端开发利器:Promise在解决异步问题中的作用与优势</span>

                        </div>
        <div class="wzconShengming">
            <img src="">
            <div>本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn</div>
        </div>

                    <div class="wzconZzwz">
            <div class="wzconZzwztitle">作者最新文章</div>
            <ul>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            C语言网络编程中并发编程的疑难解答
                        </div>
                        <div>2024-10-10 10:00:04</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            面向对象编程中的并发编程和线程安全
                        </div>
                        <div>2024-10-10 09:33:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            age动漫动画官方正版入口 age动漫动画官网地址
                        </div>
                        <div>2024-10-10 09:26:00</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            C语言网络编程中Web服务器开发的最佳实践
                        </div>
                        <div>2024-10-10 09:00:03</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            C语言面向对象编程:实战项目中常见问题的处理问答
                        </div>
                        <div>2024-10-10 08:42:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            Java 方法重载常见问题解答
                        </div>
                        <div>2024-10-09 22:45:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            独立游戏爱好者必备:十大不容错过的 Switch 独立游戏
                        </div>
                        <div>2024-10-09 22:03:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            在 Java 函数异常处理中使用设计模式有哪些好处?
                        </div>
                        <div>2024-10-09 21:42:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            php 函数缓存技术详解:函数缓存技术的未来发展趋势是什么?
                        </div>
                        <div>2024-10-09 21:00:02</div>
                    </li>
                                        <li>
                        <div class="wzczzwzli">
                            <span class="layui-badge-dots"></span>
                            php正则表达式如何处理分组和子模式?
                        </div>
                        <div>2024-10-09 20:42:02</div>
                    </li>
                                </ul>
        </div>
                    <div class="wwads-cn wwads-horizontal" data-id="156" style="max-width:985px"></div>
                    <div class="wzconZzwz">
            <div class="wzconZzwztitle">最新问题</div>
            <div class="wdsyContent">
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        用了gard("admin")还需要单独验证吗?
                        在控制器中用了gard("admin")-&gt;attempt($rules)后还需要单独写查询语句验证吗?
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">P粉709840553来自于2024-09-24 11:07:51</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">0
                                <img src="" class="wdcdcirwatchi">402
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        为什么password密码在数据据值都不样
                        自动生成后在数据库password这个字段中为什么会出现值不一样,生成都是用的是:admin888
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">P粉709840553来自于2024-09-24 11:06:02</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">0
                                <img src="" class="wdcdcirwatchi">335
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        运行视频看不了啊
                        运行视频看不了啊
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">P粉109248948来自于2024-09-24 10:21:09</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">0
                                <img src="" class="wdcdcirwatchi">255
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        为啥开头不写结束符号   ?&gt;   就写一个&lt;?php
                        为啥开头不写结束符号   ?&gt;   就写一个&lt;?php
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">P粉052048067来自于2024-09-11 18:01:19</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">1
                                <img src="" class="wdcdcirwatchi">663
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        按照老师的方法不传值给TP6后台,再接收参数。出现以下错误。怎么办
                        http://localhost/index.php/admin/index/userliste 的远程资源。(原因:CORS 头缺少 'Access-Control-Al...
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">凡人来自于2024-07-09 15:20:49</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">1
                                <img src="" class="wdcdcirpli">39
                                <img src="" class="wdcdcirwatchi">2081
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        请问写php项目都要用到git吗
                        请问写php项目都要用到git吗
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">PHP中文网用户-4202961来自于2024-06-11 14:28:59</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">57
                                <img src="" class="wdcdcirwatchi">2686
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        请问能提供下源码么
                        请问能提供下源码么?想对照着看
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">周珂儿来自于2024-06-06 08:56:32</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">47
                                <img src="" class="wdcdcirwatchi">2402
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        为什么我用的laravel10不走handle文件中的render方法,无法输出断点内容
                        为什么我用的laravel10不走handle文件中的render方法,无法输出断点内容
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">许飞来自于2024-06-02 14:41:32</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">50
                                <img src="" class="wdcdcirwatchi">1862
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        小鱼AI助手免费使用!http://ai.52xuejs.cn/#/chat
                        小鱼AI助手免费使用!http://ai.52xuejs.cn/#/chat
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">会飞的汤姆来自于2024-05-28 03:09:58</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">47
                                <img src="" class="wdcdcirwatchi">2446
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                    <div class="wdsyConDiv flexRow wdsyConDiv1">
                    <div class="wdcdContent flexColumn">
                        function_exists()无法判定自定义函数
                        function test()    {        return true;    }&amp;nbsp...
                        <div class="wdcdcInfo flexRow">
                            <div class="wdcdcileft">
                                <span class="wdcdciSpan">凡人来自于2024-04-29 11:01:01</span>
                            </div>
                            <div class="wdcdciright flexRow">
                                  <img src="" class="wdcdcirdzi">0
                                <img src="" class="wdcdcirpli">64
                                <img src="" class="wdcdcirwatchi">3289
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wdsyConLine wdsyConLine2"></div>
                                </div>
        </div>
        <div class="wzconZt">
            <div class="wzczt-title">
                <div>相关专题</div>
                更多&gt;

            </div>
            <div class="wzcttlist">
                <ul>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202306/20/2023062017455159983.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>js获取数组长度的方法</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202307/04/2023070409584996422.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>js刷新当前页面</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202307/04/2023070410160418152.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>js四舍五入</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202309/01/2023090117024286721.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>js删除节点的方法</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202309/04/2023090411084899588.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>JavaScript转义字符</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202309/04/2023090411300156430.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>js生成随机数的方法</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202309/12/2023091210411962381.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>如何启用JavaScript</span> 
                    </li>
                                            <li class="ul-li">
                        <img src="https://img.php.cn/upload/subject/202309/20/2023092011360778721.jpg?x-oss-process=image/resize,m_fill,h_145,w_220"> 
                        <span>Js中Symbol类详解</span> 
                    </li>
                                        </ul>
            </div>
        </div>