对于我的生活,我无法弄清楚为什么这个.each()
功能不起作用。似乎很直接
function getThumbnail() {
$('.carousel-focus-item').each(function() {
var videoId = $(this).data('embed');
$(this).append('<img src="//img.youtube.com/vi/' + videoId + '/hqdefault.jpg />');
console.log(this);
});
}
任何想法?
<div class="carousel-focus-items">
<div class="carousel-focus-item" data-embed="hGQkp4K05kE">
</div>
<div class="carousel-focus-item" data-embed="EtNw4wLL5oQ">
</div>
<div class="carousel-focus-item" data-embed="_bZj_yxfACw">
</div>
</div>
答案 0 :(得分:2)
您在图片来源上遗漏了"
。
$('.carousel-focus-item').each(function() {
var videoId = $(this).data('embed');
$(this).append( '<img src="//img.youtube.com/vi/' + videoId + '/hqdefault.jpg" />' );
});
这是一个小提琴:
$(document).ready(function(){
getThumbnail();
});
function getThumbnail() {
$('.carousel-focus-item').each(function() {
var videoId = $(this).data('embed');
$(this).append( '<img src="//img.youtube.com/vi/' + videoId + '/hqdefault.jpg" />' );
});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="carousel-focus-items">
<div class="carousel-focus-item" data-embed="hGQkp4K05kE">
</div>
<div class="carousel-focus-item" data-embed="EtNw4wLL5oQ">
</div>
<div class="carousel-focus-item" data-embed="_bZj_yxfACw">
</div>
</div>
&#13;