jquery如何解除事件绑定?


当前第2页 返回上一页

示例:解除事件绑定

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<!doctype html><html>

  <head>

    <meta charset="utf-8">

    <title>jQuery基本操作事件绑定</title>

    <script type="text/javascript" src="js/jquery-1.7.js"> </script>

    <style type="text/css">

        p{width:200px;height:200px;border:1px solid #666;}

        #leftp{float:left; margin:0 auto;}

        #rightp{float:right;}

    </style>

  </head>

  <body>

    <p id="leftp">

        <input type="button" value="bind事件绑定" id="bindBtn"/>

        <input type="button" value="多事件绑定" id="manyBindBtn"/>

        <input type="button" value="delegate事件绑定" id="delegateBindBtn"/>

        <input type="button" value="解除事件绑定" id="removeBindBtn"/>

    </p>

    <p id="rightp">右侧展示区</p>

    <script type="text/javascript">

        $(function(){

            //使用bind()方法绑定事件

            $("#manyBindBtn").bind({

                click:function(){$("#rightp").slideToggle();},

                mouseover:function(){$("#rightp").css("background-color","red");},

                mouseout:function(){$("#rightp").css("background-color","yellow");}

            });

            //使用delegate()方法绑定事件

            $(document).delegate("#delegateBindBtn","click",function(){

                $("#rightp").slideToggle();

            });

            //使用hover()方法绑定事件

            $("#rightp").hover(function(){

                $(this).css("background-color","gray");

            },function(){

                $(this).css("background-color","white");

            });

            //使用on()方法绑定事件

            $("#leftp").on("click","#bindBtn", function(){

                alert("使用bind()方法绑定事件处理");

            });

            //解除事件绑定

            $("#removeBindBtn").on("click",function(){

                //1.使用unbind()解除click事件绑定

                //$("#manyBindBtn").unbind("click");

                //2.使用unbind()解除该元素上的所有事件绑定

                //$("#manyBindBtn").unbind();

                //3.使用off()方法解除bind()方法的click事件绑定

                $("#manyBindBtn").off("click");

                //$(document).off("click","#manyBindBtn");

                //4.使用off()方法解除该元素上的所有事件绑

                //$("#manyBindBtn").off();             

                //5.使用undelegate()方法解除delegate()方法绑定事件

                //$(document).undelegate("#delegateBindBtn","click");

                //6.使用off()方法解除delegate()方法绑定事件

                $(document).off("click","#delegateBindBtn");

                //7.使用off()方法解除on()方法的click事件绑定

                $("#leftp").off("click","#bindBtn");

                //8.使用off()方法解除所有按钮上的所有事件绑定

                $("input[type=button]").off();

            });

        });

    </script>

  </body></html>

在这里插入图片描述

更多编程相关知识,请访问:编程课程!!

以上就是jquery如何解除事件绑定?的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

jQuery与zepto的异同有哪些

vs如何安装jQuery

判断一个对象是否为jQuery对象的方法是什么

jQuery中slider是什么?

jQuery $(document).ready()和onload的区别是什么

jQuery val()获取不到值怎么办

如何使用jQuery实现全选和全不选功能

jQuery怎么绑定click事件

jQuery更改img元素的src属性的方法

jQuery判断是否为ie浏览器的方法

更多相关阅读请进入《jQuery》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...