2013-02-27 83 views
0

我正在做一些代码插入图像到div,并加载后,用户可能能够通过点击图像触发事件,这不工作由于一些奇怪的原因,这里是代码:jquery - 加载一个图像,然后触发一个事件与该图像

$(function(){ 
    getPictures('pictures.xml'); 

    function getPictures(picturesXML){ 
     $.get(picturesXML, function(data){ 

      var pictures = data;  
      var countElements = $(pictures).find('pic').length;    

      $(pictures).find('pic').each(function(i){ 

       var img = $('<img src="images/' + $(this).attr('src') + '" style="top : ' + $(this).attr('top') + 'px; left: ' + $(this).attr('left') + 'px" width=" '+ $(this).attr('w') +'"/>'); 

        img.load(function(){ 
         $('.space').append($(this)); 
         $(this, 'space').delay(100*i).fadeIn('fast'); 
        });         

      })  

      $('.space img').mouseenter(function(){ 
       alert('hello'); 

      }) 

     }) 
    } 
}) 

有没有人可以帮我搞清楚这一点。 谢谢!

回答

1

定期的jQuery事件函数往往不支持事件冒泡。尝试使用$ .on方法。在这种情况下,更换:

$('.space img').mouseenter(function(){ 
      alert('hello'); 

     }) 

有:

$(document).on('mouseenter','.space img', function(){ 
      alert('hello'); 

     }); 

另外,还要注意缺少的分号。这应该做的伎俩。

+0

作品非常好... tx! – MariaZ 2013-02-28 00:03:33

相关问题