gpt4 book ai didi

javascript - 页面加载后弹出 youtube 视频

转载 作者:行者123 更新时间:2023-11-30 20:37:47 26 4
gpt4 key购买 nike

我想知道我们如何在页面加载后弹出模态视频。我试图编辑我从 codepen.io 获得的这段代码

$(document).ready(function(){
$("#myModal").on("hidden.bs.modal",function(){
$("#iframeYoutube").attr("src","#");
})
})

function changeVideo(vId){
var iframe=document.getElementById("iframeYoutube");
iframe.src="https://www.youtube.com/embed/"+vId;

$("#myModal").modal("show");
}
<!DOCTYPE html>
<html lang="en" >

<head>
<meta charset="UTF-8">
<title>youtube video - bootstrap modal popup</title>





</head>

<body>

<script src="https://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<a onclick="changeVideo('OMDJY6Fh9YY')">video-1</a>
<a onclick="changeVideo('OMDJY6Fh9YY')">video-2</a>



<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">

<iframe id="iframeYoutube" width="560" height="315" src="https://www.youtube.com/embed/OMDJY6Fh9YY" frameborder="0" allowfullscreen></iframe>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>



</body>

</html>

问题是我希望在页面加载后弹出窗口自动显示(抱歉我的英语不好。请纠正我的语法和句子)谢谢

最佳答案

你可以试试这个

<iframe id="iframeYoutube" width="100%" height="315" src="https://www.youtube.com/embed/2EVphJyNYTE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

只需用那个代码更改代码中的 iframe。应该是这样的

<!DOCTYPE html>
<html lang="en" >

<head>
<meta charset="UTF-8">
<title>youtube video - bootstrap modal popup</title>





</head>

<body>

<script src="https://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<a onclick="changeVideo('OMDJY6Fh9YY')">video-1</a>
<a onclick="changeVideo('OMDJY6Fh9YY')">video-2</a>



<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">

<iframe id="iframeYoutube" width="100%" height="315px" src="https://www.youtube.com/embed/2EVphJyNYTE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>



</body>
<script>
$(document).ready(function(){
changeVideo('OMDJY6Fh9YY')
})

function changeVideo(vId){
var iframe=document.getElementById("iframeYoutube");
iframe.src="https://www.youtube.com/embed/"+vId;

$("#myModal").modal("show");
}
</script>

</html>

将宽度设置为 100% 会提高响应速度。试试吧!

关于javascript - 页面加载后弹出 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com