在我的網址中,我想檢查自動播放是否存在,然後替換值。 在我的iframe中,我有網址https://www.youtube.com/embed/C0DPdy98e4c?width=640
在那個網址中,我想檢查自動播放是否存在。如果不需要在playbutton點擊中追加autoplay = 1。檢查值並追加給定值
jQuery(document).ready(function($) {
$('.playButton').click(function() {
alert('play');
$('.flex-active-slide iframe').contents().find("iframe")[0].src += "&autoplay=1"; //need to check autoplay and append with src
$("iframe").src = src.replace(/\&autoplay\=\d/, "&autoplay=" + 1);
});
$(".close_icon").click(function() {
alert('close');
$("iframe").src = src.replace(/\&autoplay\=\d/, "&autoplay=" + 0);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flex-active-slide">
<iframe src="https://www.youtube.com/embed/C0DPdy98e4c?width=640"></iframe>
</div>
<div class="playButton">Play</div>
<div class="close_icon">Close</div>
你可以使用iframe API,而不是重新加載源和節省帶寬。 –
https://developers.google.com/youtube/iframe_api_reference – Brad