2014-09-23 61 views
0

我有一些問題選擇由JS動態注入的一系列鏈接。基本上,YouTube列表已構建並加載到DOM中。現在我想添加更多的flare(沒有CSS動畫,我的挑戰在這裏,測試動態jQuery頁面上的限制真的很有趣)在DOM之後加載的元素上使用.each

但是它只是不會選擇在DOM之後加載的元素我嘗試了大量的搜索解決方案無濟於事。雖然我有一對熊,但到目前爲止我已經過了1分鐘的生日。耶,祝你生日快樂!

這裏是我想,你可以看到在行動它(而失敗)here

 $('.featured-video-link').each(function(){ // featured-video-links is added after DOM too 
      $(this).live({ 
       mouseenter: function() { 
        $(this).find('.feat-vid-title').animate({ 'backgroundColor': 'rgba(62, 132, 210, 0.8)', 'color': 'rgba(255,255,255,1.0)' }, 'slow'); 
       }, 
       mouseleave: function() { 
        $(this).find('.feat-vid-title').animate({ 'backgroundColor': 'rgba(0,0,0,0.5)', 'color': 'rgba(255,255,255,0.8)' }, 'slow'); 
       } 
      });   
     }); 

回答

1

您可以使用下面的語法,你不需要循環,

$(document).on({ 
    mouseenter: function() { 
     $(this).find('.feat-vid-title').animate({ 
      'backgroundColor': 'rgba(62, 132, 210, 0.8)', 
      'color': 'rgba(255,255,255,1.0)' 
     }, 'slow'); 
    }, 
    mouseleave: function() { 
     $(this).find('.feat-vid-title').animate({ 
      'backgroundColor': 'rgba(0,0,0,0.5)', 
      'color': 'rgba(255,255,255,0.8)' 
     }, 'slow'); 
    } 
}, '.featured-video-link'); 
+1

這是什麼巫術魔法!哈哈非常感謝,我看了'.on()'很多次,並沒有意識到你沒有__need__事件 – WASasquatch 2014-09-23 07:11:17

+0

@WASasquatch,那麼希望你會接受答案:) – 2014-09-23 07:14:05

+1

對不起,我正在等待15分鐘,然後睡着了。 :P – WASasquatch 2014-09-23 16:34:21

1

你不必遍歷每個元素綁定事件。簡單地做這樣的

$(".featured-video-link").hover(
    function() { 
     $(this).find('.feat-vid-title').animate({ 'backgroundColor': 'rgba(62, 132, 210, 0.8)', 'color': 'rgba(255,255,255,1.0)' }, 'slow'); 
    }, function() { 
     $(this).find('.feat-vid-title').animate({ 'backgroundColor': 'rgba(0,0,0,0.5)', 'color': 'rgba(255,255,255,0.8)' }, 'slow'); 
    } 
); 
相關問題