zoukankan      html  css  js  c++  java
  • 09 jQuery事件委托&小米购物车

    小米购物车

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>小米购物车</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
            }
            .shopCart{
                position: absolute;
                height: 50px;
                width: 100px;
                background: #ff6700;
                cursor: pointer;
                top:100px;
                left: 500px;
                text-align: center;
                line-height: 50px;
            }
            .content{
                position: relative;
                height: 200px;
                width: 500px;
                background: #2aabd2;
                display: none;
            }
        </style>
    </head>
    <body>
        <div>
            <div class="shopCart">购物车
                <div class="content"></div>
            </div>
    
        </div>
        <script type="text/javascript" src="jquery-3.3.1.js"></script>
        <script type="text/javascript">
            $(function (e) {
                /*
                $('.shopCart').mouseenter(function (e) {
                    $('.content').stop().slideDown(500);
                    e.stopPropagation();
                });
                $('.shopCart').mouseleave(function (e) {
                    $('.content').stop().slideUp(500);
                    e.stopPropagation();
                });
                */
                // 合成事件  mouseenter mouseleave   hover(funOver,funOut)
                $('.shopCart').hover(function (e) {
                    $('.content').stop().slideDown(500);
                    e.stopPropagation();
                },function (e) {
                    $('.content').stop().slideUp(500);
                    e.stopPropagation();
                });
            })
        </script>
    </body>
    </html>

    事件委托

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>事件委托</title>
        <style type="text/css">
            .box{
                position: absolute;
                top: 100px;
                left: 200px;
                height:200px;
                width: 300px;
                background-color: #ff6700;
                }
            ul{
                position:relative;
                list-style: none;
            }
            li{
                position:relative;
                cursor: pointer;
                top: 20px;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <button>添加</button>
            <ul>
                <li>晓钢</li>
                <li>晓红</li>
                <li>晓名</li>
            </ul>
        </div>
        <script type="text/javascript" src="jquery-3.3.1.js"></script>
        <script type="text/javascript">
            $(function (e) {
                $('button').click(function (e) {
                    $('ul').append('<li>晓均</li>');
                });
    
                // 么有事件委托的情况下,当新增加元素li时,li不会绑定li的事件
                /*
                $('li').click(function (e) {
                    // e.stopPropagation();
                    alert($(e.target).text());
                });
                */
    
                // 用事件委托的情况下,当新增加元素li时,li会绑定li的事件  等于li的事件委托给了ul,
                // 不论追加多少li,都会绑定这个委托的事件
                $('ul').on('click',$('ul>li'),function (e) {
                    alert($(e.target).text());
                    e.stopPropagation();
                });
            })
        </script>
    </body>
    </html>
  • 相关阅读:
    五、K3 WISE 开发插件《直接SQL报表开发新手指导
    四、K3 WISE 开发插件《工业单据老单插件开发新手指导》
    三、K3 WISE 开发插件《K3 WISE开发手册》
    二、K3 WISE 开发插件《 工业单据老单客户端插件事件、属性、方法》
    一、K3 WISE 开发插件《K3 WISE常用数据表整理》
    首次安装金蝶注意事项
    金蝶系统,反写采购价格管理资料状态怎么选择?
    网络编程----socket套接字的黏包问题
    网络编程----socket套接字
    网络编程----网络协议篇(osi七层协议)
  • 原文地址:https://www.cnblogs.com/znyyy/p/11119681.html
Copyright © 2011-2022 走看看