2017-01-31 9 views
0

属性 'autoplay'がiframe urlに存在するかどうかをチェックします。属性が存在しない場合は、「自動再生」を追加します。それが存在する場合は、 'autoplay = 1'の値を 'autoplay = 0'に変更したい場合とその逆の場合もあります。iframe src内にautoplay属性が存在することを確認してください

私は動的なsrcを持つiframeを持っています。今すぐプレイアイコンをクリックすると、自動再生属性がhttps://www.youtube.com/embed/fffjZpMIHk?autoplay=1?autoplay=1と追加されます。だから私は、属性が存在するかどうかを確認してから、追加する必要があります。

<div class="video-container" id="video-container"> 
    <iframe src="https://www.youtube.com/embed/fffjZpMIHk" frameborder="0" allowfullscreen=""></iframe> 

私が今使っているスクリプト:

$(".playButton").on('click',function() { 
    $(".flex-active-slide iframe")[0].src += "?autoplay=1"; 
    $('.flex-active-slide').contents().find("iframe").attr('src', function(i, oldSrc) { 
    return oldSrc.replace('autoplay=0', 'autoplay=1'); 
    }); 
}); 

サンプルフィドル:Demo

+0

が質問にこのjsfiddleを編集して、あなたのコードサンプルを共有する - http://jsfiddle.net/ifinto/o2gxgz9r/ –

+0

私の更新のポストをチェックしてください – user3386779

答えて

2

あなたはコードの下で試してくださいでした:

$(function() { 
    $(".playButton").on('click',function() { 
     var iframeObj = $(".flex-active-slide iframe")[0]; 
     var currentURL = iframeObj.src; 

     if(currentURL.indexOf("autoplay=") == -1) 
     { 
      //autoplay not present in src; so lets add it 
      currentURL = currentURL + '?autoplay=1' 
     } 
     else{ 
      if(currentURL.indexOf("autoplay=1")>-1){ 
       //replace autoplay=1 with autoplay=0 
       currentURL = currentURL.replace('autoplay=1', 'autoplay=0'); 
      } 
      else 
      { 
       //replace autoplay=0 with autoplay=1 
       currentURL = currentURL.replace('autoplay=0', 'autoplay=1'); 
      } 
     } 

     iframeObj.src = currentURL; //set the newer src to iframe 

    }); 
}); 
0

match Stringメソッドを使用できます。以下を確認してください。

var isAutoPlay = "https://www.youtube.com/embed/jebJ9itYTJE?autoplay=0".match(/autoplay=([^&]*)/); 
if (isAutoPlay && isAutoPlay[1] == 1) { 
    return oldSrc.replace('autoplay=0', 'autoplay=1'); 
} 

ワーキングサンプル:https://jsfiddle.net/ramsunvtech/dyutu19f/3/

関連する問題