zoukankan      html  css  js  c++  java
  • angularjs数据异步加载时的绑定事件

           // 顶级菜单项的鼠标移入和移出操作
                $(document).on({
                    mouseover: function () {
                        var top = $(this).position().top + 24;
                        var left = $(this).position().left + 10;
    
                        if (top + $(this).children('ul').outerHeight(true) > $(window).height()) {
                            top -= $(this).children('ul').outerHeight(true) + 20;
                        }
    
                        $(this).children('ul').css({
                            top: top,
                            left: left
                        }).slideDown('fast');
                    },
                    mouseleave:function () {
                        $(this).children('ul').slideUp('fast');
                    }
                }, ".menu>ul>li");

    在需要为较多的元素绑定事件的时候,优先考虑事件委托,可以带来性能上的好处。

    一、这种写法是把事件全部委托到document元素上,然后通过事件冒泡来执行函数,可以在同一类型的元素上绑定多个事件,只有当元素类型是指定的".menu>ul>li"时才会触发事件。

    二、当指定class的元素未被加载时,js也不会报错。

  • 相关阅读:
    Delphi语法
    orcad中注意的事情
    Express web框架
    Docker
    Node.JS
    再次 WebAssembly 技术探讨
    WebAssembly 浏览器中运行c/c++模块
    Http 服务 简单示例
    CentOS7 开放服务端口
    Go linux 实践4
  • 原文地址:https://www.cnblogs.com/len0031/p/4874041.html
Copyright © 2011-2022 走看看