【发布时间】:2019-12-23 07:05:01
【问题描述】:
我有一个网页,上面有一个图片轮播,我没有将 youtube 视频嵌入到轮播中,而是放置了一个带有 youtube 图标的图片,然后单击该模式打开了 youtube 视频。
由于轮播是动态提供图像和 youtube 链接的,因此我已将 youtube 链接放在触发模式的 div 中。
问题是模式打开但“show.bs.modal”或“hide.bs.modal”事件不起作用,因此永远不会获得 youtube 视频链接。
HTML
<!--Carousel-->
<div id="carouselIndicatorsDiv" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators filterInvert1">
</ol>
<div class="carousel-inner">
</div>
<a class="carousel-control-prev" href="#carouselIndicatorsDiv" role="button" data-slide="prev">
<span class="carousel-control-prev-icon filterInvert1" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselIndicatorsDiv" role="button" data-slide="next">
<span class="carousel-control-next-icon filterInvert1" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!--Modal-->
<div class="modal fade" id="ytSubsModal" tabindex="-1" role="dialog" aria-labelledby="ytSubsModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="" id="ytSubsVideo" allowscriptaccess="always" allow="autoplay"></iframe>
</div>
</div>
</div>
</div>
</div>
JQuery
//Appended other images, appending image for modal click
$(".carousel-inner").append('<div id="ytSrcLink" class="carousel-item cursorPointer" data-toggle="modal" data-target="#ytSubsModal" src="https://www.youtube.com/embed/HHncD0yt71c?autoplay=1"><img class="d-block position-relative w-100" src="img/1/thumbnail.jpg"><img src="img/youtubeIcon.svg" class="position-absolute center youtubeIconStyle"></div>');
// Modal
var videoYtSubsSource;
$("#ytSrcLink").on('click', function(){
videoYtSubsSource = $(this).attr('src');
assignYtSubsSrc(videoYtSubsSource);
});
// Youtube Modal
var ytSubsSrc;
function assignYtSubsSrc(videoUrl){
ytSubsSrc=videoUrl;
}
$(document).on('show.bs.modal', "#ytSubsModal", function(e) {
$('#ytSubsVideo').attr('src',ytSubsSrc);
});
$(document).on('hide.bs.modal', "#ytSubsModal", function(e) {
$('#ytSubsVideo').attr('src','');
});
我什至尝试直接将模态触发 div 放入 HTML 中,然后尝试,但这也没有用。 我想我缺少一些基本的东西,但不确定是什么。
【问题讨论】:
-
如果您包含一个重现问题的最小工作示例,它会更容易提供帮助
-
@AkberIqbal 感谢您让我的代码成为一个工作示例,它让我探索我做错了什么,因为代码独立工作。您能看看我发布的答案并进一步指导我吗?
标签: jquery html bootstrap-4 bootstrap-modal bootstrap-carousel