Javascript 在刚刚插入的HTML中找不到类jQuery的元素
Javascript 在刚刚插入的HTML中找不到类jQuery的元素,javascript,jquery,backbone.js,underscore.js,Javascript,Jquery,Backbone.js,Underscore.js,tmpl['video-list']({videos:json})是下划线模板,包含DIVs中的项目列表这里是返回示例 render:function () { var self = this; $.ajax({url:'/db/list.json', dataType:'JSON'}).done(function (json) { var html = tmpl['video-list']({videos:json}); self.$el.htm
tmpl['video-list']({videos:json})
是下划线模板,包含DIV
s中的项目列表这里是返回示例
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));
});
});
}
为什么我在
$('.video thumb box')
中找不到任何东西?您的每个循环都是反向的
<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>
这是jQuery
.each()
页面您需要再次调用.render
$。ajax
是异步的。您需要在Ajax调用完成后执行循环。(例如,将代码移动到done
中的功能块中。这正是问题的原因。我刚刚移动了$。每个都移动到done()
中,现在一切正常。
$('.video-thumb-box').each(function() {
console.log(this);
$(this).bind('mouseenter', function(){
console.log($('.video-thumb-info', this));
});
});