2013-02-17 78 views
2
render:function() { 
    var self = this; 
    $.ajax({url:'/db/list.json', dataType:'JSON'}).done(function (json) { 
     var html = tmpl['video-list']({videos:json}); 
     self.$el.html(html); 
    }); 

    console.log($('.video-thumb-box')); 

    $.each($('.video-thumb-box'), function(){ 
     console.log(this); 
     $(this).bind('mouseenter', function(){ 
      console.log($('.video-thumb-info', this)); 
     }); 
    }); 
} 

tmpl['video-list']({videos:json})是下劃線模板,包含DIV的項目進行列表這裏例如返回無法找到剛剛插入HTML由類的jQuery元素

<div class="span3"> 
    <a href="#/video/123"> 
     <div class="video-thumb-box"> 
      <img class="video-thumb-img" src="test" alt="Video tumbnail"> 
      <div class="video-thumb-info hide"> 
       <img class="pull-right" width="16" height="16" src="/img/icons/namba.png"> 
       something 
      </div> 
     </div> 
    </a> 
</div> 
<div class="span3"> 
    <a href="#/video/123"> 
     <div class="video-thumb-box"> 
      <img class="video-thumb-img" src="test" alt="Video tumbnail"> 
      <div class="video-thumb-info hide"> 
       <img class="pull-right" width="16" height="16" src="/img/icons/namba.png"> 
       something 
      </div> 
     </div> 
    </a> 
</div> 

爲什麼我無法找到任何$('.video-thumb-box')

+0

你需要調用'.render'再次 – 2013-02-17 16:55:42

+1

'$ .ajax'是異步。您需要在Ajax調用完成後執行循環。 (例如,將代碼移到'done'中的功能塊中 – WiredPrairie 2013-02-17 17:04:48

+0

這就是問題的原因,我剛將'$ .each'移到'done()'中,現在一切正常。 – 2013-02-17 17:07:55

回答

2

你的每個循環都是向後的。

$('.video-thumb-box').each(function() { 
    console.log(this); 
    $(this).bind('mouseenter', function(){ 
     console.log($('.video-thumb-info', this)); 
    }); 
}); 

這裏是jQuery的.each()API

相關問題