-jq-on注册事件

01-jq-on注册事件

<!DOCTYPE html>
<html lang="zh-CN">

<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>
    <style>
        .current {
            /* height: 100px; */
            background-color: rebeccapurple;
        }
    </style>

    <script src="../js/jquery-1.12.4.min.js"></script>
</head>

<body>
    <ul>
        <li>隔壁老王1</li>
        <li>隔壁老王2</li>
        <li>隔壁老王3</li>
        <li>隔壁老王4</li>
        <li>隔壁老王5</li>
        <li>隔壁老王6</li>
        <li>隔壁老王7</li>
        <li>隔壁老王8</li>
        <li>隔壁老王9</li>
        <li class="current">隔壁老王10</li>
    </ul>
    <script>
        $(function () {
            // on的简单注册
            // $('ul').on('click', function () {
            //     console.log(this)
            //     $(this).css({ backgroundColor: 'green' })
            // })

            // on的委托事件
            /*  $('ul').on('click', 'li', function () {
                 console.log(this)
                 $(this).css({ backgroundColor: 'green' })
             }) */

            // on注册多个事件:
            $('ul').on({
                mouseover: function () {
                    $(this).css({ backgroundColor: 'green' })
                },
                mouseout: function () {
                    $(this).css({ backgroundColor: '' })
                },
                click: function () {
                    $(this).css({ color: 'yellow' })
                }
            }, 'li')

            // on可以注册任何事件

            // 1. 常用的:简单事件,on('事件类型:1个',回调函数)
            // 2. 事件委托:on('事件类型','目标子元素的选择器',回调函数)
            // 3. 同时多个事件:共用一个回调函数:on('事件1 事件2 ...',回调函数):比较多的切换效果toggle系列的
            // $('ul').children('li:last').on('mouseover mouseout', function () {
            //     console.log(this)
            //     $(this).toggleClass('current')
            // })
            // 4. 同时注册多个事件:每个回调函数都不同:on({事件1:回调函数1,事件2:回调函数2...})
        })
    </script>
</body>

</html>

注意:

// on可以注册任何事件

        // 1. 常用的:简单事件,on('事件类型:1个',回调函数)
        // 2. 事件委托:on('事件类型','目标子元素的选择器',回调函数)
        // 3. 同时多个事件:共用一个回调函数:on('事件1 事件2 ...',回调函数):比较多的切换效果toggle系列的
        // $('ul').children('li:last').on('mouseover mouseout', function () {
        //     console.log(this)
        //     $(this).toggleClass('current')
        // })
        // 4. 同时注册多个事件:每个回调函数都不同:on({事件1:回调函数1,事件2:回调函数2...})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值