zoukankan      html  css  js  c++  java
  • jQuery事件之on()方法绑定多个选择器,多个事件

    $(document).on('click', '#header .top, #main .btn', function () {
         // code...
     });
    

    on()方法绑定多个事件

    $(".box1").on({
        mouseenter: function() {
            // Handle mouseenter...
        },
        mouseleave: function() {
            // Handle mouseleave...
        },
        click: function() {
            // Handle click...
        }
    }, "p");
    

    $(".box1 p").on({
        mouseenter: function() {
            // Handle mouseenter...
        },
        mouseleave: function() {
            // Handle mouseleave...
        },
        click: function() {
            // Handle click...
        }
    });
    

    用on()方法绑定多个选择器、多个事件

    $('#header .top, #main .btn').on({
        mouseenter: function() {
            // Handle mouseenter...
        },
        mouseleave: function() {
            // Handle mouseleave...
        },
        click: function() {
            // Handle click...
        }
    }); 
    

    附上测试小demo

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
        div {
             200px;
            height: 200px;
        }
        
        .box1 p {
            background-color: #009cff;
        }
        
        .box2 .p2 {
            background-color: #f40;
        }
        </style>
    </head>
    
    <body>
        <div class="box1">
            这是第一个
            <p class="p1">11111</p>
            <p>22222</p>
            <p>33333</p>
            <p>55555</p>
        </div>
        <div class="box2">
            <p class="p2">这是第二个</p>
        </div>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            // 绑定多个事件
            // $(".box1 p").on({
            //     mouseenter: function() {
            //         $(this).css('background-color','#ccc');
            //     },
            //     mouseleave: function() {
            //         $(this).css('background-color','#009cff');
            //     },
            //     click: function() {
            //         $(this).css('background-color','#f40');
            //     }
            // });
            // 绑定多个选择器、多个事件
            $('.box1 .p1,.box2 .p2').on({
                mouseenter: function() {
                    $(this).css('background-color', '#ccc');
                },
                mouseleave: function() {
                    $(this).css('background-color', '#009cff');
                },
                click: function() {
                    $(this).css('background-color', '#f40')
                }
            });
        });
        </script>
    </body>
    </html>
    
  • 相关阅读:
    Lucene学习总结之七:Lucene搜索过程解析
    Lucene学习总结之六:Lucene打分公式的数学推导
    Lucene学习总结之五:Lucene段合并(merge)过程分析
    Lucene学习总结之四:Lucene索引过程分析
    Lucene学习总结之三:Lucene的索引文件格式(1)
    Lucene学习总结之二:Lucene的总体架构
    Lucene学习总结之一:全文检索的基本原理
    解决Eclipse中文乱码
    【Lucene4.8教程之五】Luke
    【Tika基础教程之一】Tika基础教程
  • 原文地址:https://www.cnblogs.com/xjuan/p/5439439.html
Copyright © 2011-2022 走看看