代码之家  ›  专栏  ›  技术社区  ›  Atif

jquery绑定不适用于Ajax加载的HTML

  •  0
  • Atif  · 技术社区  · 16 年前

    这是我的jquery代码

    jQuery(document).ready (function() {
    
       // post
        $('.post').bind('mouseenter mouseleave', function() {
            $(this).filter('.btn').toggleClass('hidden');
        });
    
    
    });
    

    它在普通文档上工作得很好。但是,当我加载一些HTM(即一些带有.post属性的div)时,使用Ajax并将其嵌入到我的DOM中。

    上面的代码不适用于这些div。

    2 回复  |  直到 16 年前
        1
  •  3
  •   PetersenDidIt    16 年前

    试用使用 live :

    jQuery(document).ready (function() {
        $('.post').live('mouseenter mouseleave', function() {
            $(this).filter('.btn').toggleClass('hidden');
        });
    });
    

    还是更好 delegate

    jQuery(document).ready (function() {
        $('#posts').delegate('.post','mouseenter mouseleave', function() {
            $(this).filter('.btn').toggleClass('hidden');
        });
    });
    
        2
  •  2
  •   edl    16 年前

    你的时间安排可能会被取消 .post 属性必须存在于DOM中,然后才能将任何内容绑定到它。