代码之家  ›  专栏  ›  技术社区  ›  Sara Chipps

Live()关键字无法加载动态html图像

  •  5
  • Sara Chipps  · 技术社区  · 14 年前

    我有动态添加到页面的图像,我似乎无法使用live()使“load”事件动态工作。

    $('#largeImg' + nextUniqueItemID).hide();
    $('#largeImg' + nextUniqueItemID).live('load' , function() {
        $('#loader' + nextUniqueItemID).hide();
        $('#largeImg' + nextUniqueItemID).show();
    });
    

    '#largeImg' + nextUniqueItemID 在函数和

    我觉得我可能误用了“live”,因为它并不需要一个听众,而是需要立即触发事件。

    谢谢你的帮助。我试过“绑定”,但从未触发。我也试着摆脱了负荷,但那不起作用。有没有一种方法可以将侦听器附加到一个事件,以指示何时完成图像加载?

    5 回复  |  直到 14 年前
        1
  •  6
  •   Anurag    14 年前

    我认为jQuery文档在这一点上可能有点误导,除非它正在做一些非常特别的事情来解决这个问题。

    1 , 2

    既然它们不冒泡 document

    因为

    例如,假设您要注册以下实时事件:

    $("#container > input").live("change", function() {
        alert("changed");
    });
    

    $(document).bind("change", function() {
        ..
    });
    

    当文档中出现任何更改事件时,它将遍历每个已注册的选择器,并检查给定选择器的任何结果节点是否包含事件目标(事件源自的元素)。如果存在,则为该事件目标触发相应的“活动”事件处理程序。

    除非我弄错了,而且jQuery对此有一些秘密,否则应该直接绑定load事件,而不是通过live进行绑定。

    $("selector").bind("load", function() { .. });
    
        2
  •  1
  •   Reigel Gallarde    14 年前

    这个代码在哪里??

    $('#largeImg' + nextUniqueItemID).hide();
    $('#largeImg' + nextUniqueItemID).live('load' , function() {
    $('#loader' + nextUniqueItemID).hide();
    $('#largeImg' + nextUniqueItemID).show();
    });
    

    如果这在jqueryajax的回调中,则不必使用 .live() .bind() .

        3
  •  0
  •   Kerry Jones    14 年前

    http://api.jquery.com/live/

    * In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live(): click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.
    
        * As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events. As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout).
        * As of jQuery 1.4.1 the hover event can be specified (mapping to "mouseenter mouseleave").
    

    无论触发创建的是什么,都应该触发正在使用的项目的隐藏和显示 load 事件。

        4
  •  0
  •   tanathos    14 年前

    $('#largeImg' + nextUniqueItemID).hide();
    $('[id=largeImg' + nextUniqueItemID + ']').live('load', function() {
        $('#loader' + nextUniqueItemID).hide();
        $('[id=largeImg' + nextUniqueItemID + ']').show();
    });
    
        5
  •  -1
  •   Sara Chipps    14 年前

    我最后做的是将load事件直接放到我的映像的onLoad=“”事件中。结果是这样的。