前端开发-jQuery事件

一、什么是事件

页面对不同访问者的响应叫做事件。

事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。

实例:

  • 在元素上移动鼠标。
  • 选取单选按钮
  • 点击元素

在事件中经常使用术语"触发"(或"激发")例如: "当您按下按键时触发 keypress 事件"。

常见 DOM 事件

 

二、jQuery事件方法语法

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

页面中指定一个点击事件:

$("p").click();

  下一步是定义什么时间触发事件。您可以通过一个事件函数实现:

$("p").click(function(){        
       
});

 

三、常用的 jQuery 事件方法

  • $(document).ready()

    $(document).ready() 方法允许我们在文档完全加载完后执行函数。

  • click()

    click() 方法是当按钮点击事件被触发时会调用一个函数。

    该函数在用户点击 HTML 元素时执行。

在下面的实例中,当点击事件在某个 <p> 元素上触发时,隐藏当前的 <p> 元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("p").click(function () {
                $(this).hide();

            });

        });
    </script>
</head>
<body>
<p>如果你点我,我就会消失。</p>
<p>点我消失!</p>
<p>点我也消失!</p>

</body>
</html>
  •   dblclick()

    当双击元素时,会发生 dblclick 事件。

    dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数:

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("p").dblclick(function () {
                $(this).hide();

            });

        });
    </script>
</head>
<body>
<p>双击鼠标左键的,我就消失。</p>
<p>双击我消失!</p>
<p>双击我也消失!</p>

</body>
</html>

  

  • mouseenter()

    当鼠标指针穿过元素时,会发生 mouseenter 事件。

    mouseenter() 方法触发 mouseenter 事件,或规定当发生 mouseenter 事件时运行的函数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("#p1").mouseenter(function () {
                alert("你的鼠标移动到了id=p1的元素上!")

            })

        })
    </script>
</head>
<body>
<p id="p1">鼠标指针进入此处,会看到弹窗</p>

</body>
</html>

  

  • mouseleave()

    当鼠标指针离开元素时,会发生 mouseleave 事件。

          mouseleave()方法触发mouseleave事件,或规定当发生mouseleave事件时运行的函数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("#p1").mouseleave(function () {
                alert("再见,您的鼠标已经离开了该段落")

            })

        })
    </script>
</head>
<body>
    <p id="p1">这是一个段落。</p>

</body>
</html>

  

  • mousedown()

    当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件。

    mousedown() 方法触发 mousedown 事件,或规定当发生 mousedown 事件时运行的函数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("#p1").mousedown(function () {
                alert("鼠标在该段落上按下!")

            })

        })
    </script>
</head>
<body>
    <p id="p1">这是一个段落。</p>

</body>
</html>

  

  • mouseup()

    当在元素上松开鼠标按钮时,会发生 mouseup 事件。

    mouseup() 方法触发 mouseup 事件,或规定当发生 mouseup 事件时运行的函数:

  •  

    $(document).ready(function () {
                $("#p1").mouseup(function () {
                    alert("鼠标在该段落上按下!")
    
                })
    

      

  • hover()

    hover()方法用于模拟光标悬停事件。

    当鼠标移动到元素上时,会触发指定的第一个函数(mouseenter);当鼠标移出这个元素时,会触发指定的第二个函数(mouseleave)。

 $(document).ready(function () {
            $("#p1").hover(function () {
                alert("你进入了p1!");

            },
            function () {
                alert("拜拜!现在你离开了P1")

            });

        });
  •  focus()

    当元素获得焦点时,发生 focus 事件。

    当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。

    focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js">

    </script>
    <script>
        $(document).ready(function () {
            $("input").focus(function () {
                $(this).css("background-color","#ccc");

            });
            $("input").blur(function () {
                $(this).css("background-color","#ffffff");

            });

        });
    </script>
</head>
<body>
    Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">

</body>
</html>

  

比较keypress、keydown与keyup

  • keydown:在键盘上按下某键时发生,一直按着则会不断触发(opera浏览器除外),它返回的是键盘代码;
  • keypress:在键盘上按下一个按键,并产生一个字符时发生, 返回ASCII码。注意: shift、alt、ctrl等键按下并不会产生字符,所以监听无效,换句话说,只有按下能在屏幕上输出字符的按键时keypress事件才会触发。若一直按着某按键则会不断触发。
  • keyup:用户松开某一个按键时触发,与keydown相对,返回键盘代码.
posted @ 2018-08-17 10:25  芳姐  阅读(173)  评论(0编辑  收藏  举报