jquery each()& append()函数不起作用

时间:2018-01-20 05:05:50

标签: jquery

对于我的生活,我无法弄清楚为什么这个.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>

1 个答案:

答案 0 :(得分:2)

您在图片来源上遗漏了"

 $('.carousel-focus-item').each(function() {
    var videoId = $(this).data('embed');
    $(this).append( '<img src="//img.youtube.com/vi/' + videoId + '/hqdefault.jpg" />' );
});

这是一个小提琴:

&#13;
&#13;
$(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;
&#13;
&#13;