2017-11-25 251 views
1

我列出了sql表中的视频数据。在bootstrap模式框中打开youtube视频

字段在临表: - sidebar_video_id(自动递增) - sidebar_video_nev - sidebar_video_link(完整的URL) - sidebar_video_v_id(从网址高端视频ID)

我想要的,是当我点击在每个视频中,它会打开并在引导模式框中播放。现在盒子打开,但是它是空的,我没有得到任何控制台错误。

<?php 
$get_videos = mysqli_query($kapcs, "SELECT * FROM sidebar_video"); 
if(mysqli_num_rows($get_videos) > 0) 
{ 
    while($vid = mysqli_fetch_assoc($get_videos)) 
    { 
     echo '<div class="sidebar_youtube_box">'; 
     echo '<a href="#" id="'.$vid['sidebar_video_v_id'].'" data-url="'.$vid['sidebar_video_link'].'" class="open_youtube_modal" title="'.$vid['sidebar_video_nev'].'"><img src="http://img.youtube.com/vi/'.$vid['sidebar_video_v_id'].'/hqdefault.jpg" class="img-responsive"></a>'; 
     echo '</div>'; 
    } 
} 
?> 


<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Bezárás</span></button> 
     <h4 class="modal-title" id="myModalLabel">Videó megtekintése</h4> 
     </div> 
     <div class="modal-body" id="video_modal_body"> 

     </div> 
    </div> 
    </div> 
</div> 

$('.open_youtube_modal').click(function(e) { 
     e.preventDefault(); 
     var v_id = $(this).attr('id'); 
     var full_url = $(this).attr('data-url'); 

     var embed_html = '<iframe width="560" height="315" src="' + full_url + '" frameborder="0" allowfullscreen></iframe>'; 

     //alert(embed_html); 

     $('#video_modal_body').html(embed_html); 
     $('#videoModal').modal('show'); 

    }); 
+0

'full_url'返回正确的值? /控制台或PHP错误日志中的任何错误? – Pedram

回答

0

您必须使用https://www.youtube.com/embed/[videoId]格式,而不是https://www.youtube.com/watch?v=[videoId]。 Youtube不允许与后者进行交叉起源。

+0

你好!这样可行!但是,如果我关闭模式,我该如何停止视频? – KissTom87

+0

@ KissTom87那么,这是另一个问题,但最简单的方法是在'hidden.bs.modal'事件中销毁'#video_modal_body'的内容,如下所示: $('#videoModal')。on('hidden .bs.modal',function(e){$('#video_modal_body')。html('');}) – dferenc

+0

非常感谢,它的工作! :) – KissTom87