更改iframe src而不更新页面

时间:2018-01-15 02:45:14

标签: javascript html modal-dialog bootstrap-modal

查看我的HTML代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>

<div id="myModal" class="modal fade" role="dialog" tabindex='-1'>
<div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
                <p class="modal-title">SECRETS OF SUPERIOR CUSTOMER SERVICE (July 22, 2016)</p>
         </div>
        <div class="modal-body">
            <div class="embed-responsive embed-responsive-16by9">
                <iframe class="embed-responsive-item" src="http://chishare.cc/embed/2107778410/" allowfullscreen="" width="640" height="360" frameborder="0"></iframe>
             </div>
         </div>
         <div class="modal-footer">
             <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
         </div>

    </div>
</div>

我的主要问题是:如何在没有页面刷新的情况下更改此iframe src视频? 像这样:

<button type="button" onclick="src1()">Change to video 1</button>
<button type="button" onclick="src2()">Change to video 2</button>
<button type="button" onclick="src3()">Change to video 3</button>
<button type="button" onclick="src4()">Change to video 4</button>

及其各自的职能:

function src1(){
-- Change the iframe src link of myModal
}

function src2(){
-- Change the iframe src link of myModal
}
...
你能帮帮我吗? 感谢

2 个答案:

答案 0 :(得分:0)

第1步:使用iframe的ID ...

<iframe id="FrameId"></iframe>

第2步:更新按钮......

<button type="button" onclick="ChangeSource(1);">Change to video 1</button>

第3步:包含此功能......

function ChangeSource(Button){
if(Button==1){FrameId.src='URL 1';} else 
if(Button==2){FrameId.src='URL 2';} else 
if(Button==3){FrameId.src='URL 3';} else 
if(Button==4){FrameId.src='URL 4';}
}

答案 1 :(得分:0)

我建议使用1个函数而不是4个,并将链接添加为每个按钮内的数据。您还可以在运行时动态添加这些链接。我在按钮1的演示中使用了来自youtube的占位符视频,如您所见,您可以使用以下jQuery函数切换出iframe src。

&#13;
&#13;
$( document ).ready(function() {
  $('.changer').click(function(e){
    changeIframe(e);
  })
})

function changeIframe(e) {
  var link = $(e.currentTarget).data('link');
  $('.embed-responsive-item').attr('src',link);
  alert('link is now '+link);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>
<div>
  <button class="changer" type="button" data-link="https://www.youtube.com/watch?v=ScMzIvxBSi4">Change to video 1</button>
  <button class="changer" type="button" data-link="">Change to video 2</button>
  <button class="changer" type="button" data-link="">Change to video 3</button>
  <button class="changer" type="button" data-link="">Change to video 4</button>
</div>

<div id="myModal" class="modal fade" role="dialog" tabindex='-1'>
<div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
                <p class="modal-title">SECRETS OF SUPERIOR CUSTOMER SERVICE (July 22, 2016)</p>
         </div>
        <div class="modal-body">
            <div class="embed-responsive embed-responsive-16by9">
                <iframe class="embed-responsive-item" src="http://chishare.cc/embed/2107778410/" allowfullscreen="" width="640" height="360" frameborder="0"></iframe>
             </div>
         </div>
         <div class="modal-footer">
             <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
         </div>

    </div>
</div>
&#13;
&#13;
&#13;