zoukankan      html  css  js  c++  java
  • jQuery 中bind(),live(),delegate(),on() 区别

    on()来改写通过 .bind(), .live(), .delegate()所注册的事件

    /* The jQuery .bind(), .live(), and .delegate() methods are just one 
       line pass throughs to the new jQuery 1.8.2 .on() method */
    
    // Bind
    $( "#members li a" ).on( "click", function( e ) {} ); 
    $( "#members li a" ).bind( "click", function( e ) {} ); 
    
    // Live
    $( document ).on( "click", "#members li a", function( e ) {} ); 
    $( "#members li a" ).live( "click", function( e ) {} );
    
    // Delegate
    $( "#members" ).on( "click", "li a", function( e ) {} ); 
    $( "#members" ).delegate( "li a", "click", function( e ) {} );
    

      

    • 用.bind()的代价是非常大的,它会把相同的一个事件处理程序hook到所有匹配的DOM元素上
    • 不要再用.live()了,它已经不再被推荐了,而且还有许多问题
    • .delegate()会提供很好的方法来提高效率,同时我们可以添加一事件处理方法到动态添加的元素上。
    • 我们可以用.on()来代替上述的3种方法
  • 相关阅读:
    hihocoder 1038
    hihocoder 1039
    poj 2774
    bzoj 4690&&4602
    poj 2417
    STL
    poj 1026
    poj 1064
    poj 1861(prim)
    poj 1129
  • 原文地址:https://www.cnblogs.com/tinya/p/4598298.html
Copyright © 2011-2022 走看看