微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery中的on 和事件委派

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>事件</title>
    <style>
        p{
          width:100px;
          height:100px;
          border: 1px solid gray;
        }
    </style>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script>
    $(function(){
    /*
        $('p').click(function(){
           alert($(this).text());
        });
        */
        $('p').on({'click':function(e){
              //alert(e);  //$(this).text()
              console.log(e)
        },
        'mouSEOver':function(){$(this).css('background','blue');},
        'mouSEOut':function(){$(this).css('background','white')}});

        //事件的委派
         $('div').delegate('button','click',function(){
            $(this).prev().slidetoggle();
         })
   });

    </script>
</head>
<body>
<p>AAAA</p>
<p>BBBB</p>
<p>CCCC</p>

<div style="background-color:red">
<p>这是一个段落。</p>
<button>请点击这里</button>
</div>
</body>
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐