2013-02-27 41 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

相關問題