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也不会报错。

  • 相关阅读:
    HDU 2544 最短路
    HDU 3367 Pseudoforest
    USACO 2001 OPEN
    HDU 3371 Connect the Cities
    HDU 1301 Jungle Roads
    HDU 1879 继续畅通工程
    HDU 1233 还是畅通工程
    HDU 1162 Eddy's picture
    HDU 5745 La Vie en rose
    HDU 5744 Keep On Movin
  • 原文地址:https://www.cnblogs.com/len0031/p/4874041.html
Copyright © 2011-2022 走看看