【问题标题】:Load src of iframe using drop down menu and then change使用下拉菜单加载 iframe 的 src 然后更改
【发布时间】:2014-11-28 10:41:03
【问题描述】:

我有以下代码

<script type="text/javascript">
 function newSrc() {
  var e = document.getElementById("MySelectMenu");
  var newSrc = e.options[e.selectedIndex].value;
  document.getElementById("MyFrame").src=newSrc;
 }
</script>

<iframe name="content" src="" style="position: absolute; left: 0px; top: 28px;" allowtransparency="true" border="0" scrolling="yes" width="100%" frameborder="0"     height="90%" id="MyFrame"></iframe>

<select id="MySelectMenu">
<option value="http://www.example.com">Example Site 1</option>
<option value="http://www.example2.com">Example Site 2</option>
</select>
<button onClick="newSrc();">Load Site</button>

这很好用,它根据从菜单中选择的选项加载 iframe 的源代码。现在,我想做的是,在从下拉列表中选择选项并单击“加载站点”按钮后,将选项中的源加载到 iframe 中,然后在说 ...1 后重定向 iframe ONCE第二个。

所以如果选择了示例站点 2,用户点击加载站点,http://www.example2.com 被加载,然后 iframe 被重定向到 http://www.example2.com/admin

谢谢

【问题讨论】:

  • 我可以为您提供解决方案。你介意使用 jQuery 吗?
  • @punkbit 一点也不,我不介意。 jQuery会很好:)

标签: javascript html iframe


【解决方案1】:

只是想帮忙!这是一个解决方案(工作示例,您可以使用代码http://jsbin.com/cakuh/1/),这只是一个草图,但您应该了解该怎么做:

  <iframe name="content" src="" style="position: absolute; left: 0px; top: 28px;" allowtransparency="true" border="0" scrolling="yes" width="100%" frameborder="0"     height="90%" id="MyFrame"></iframe>

  <select id="MySelectMenu">
  <option value="http://www.games.com">Games</option>
  <option value="http://www.bbc.com">BBC</option>
  </select>
  <button>Load Site</button>

$(function(){

  $('button').on('click', function(){
     var src = $("#MySelectMenu option:selected").attr('value'),
         myTimeout = null;  

    // first we de-attach and re-attach an event load
    $('iframe[name="content"]').off('load').on('load', function(){
     myTimeout = setTimeout(function(){

       clearTimeout(myTimeout);
       $('iframe[name="content"]').off('load');

       // you see, we concat '/admin' to the src
       $('iframe[name="content"]').attr('src', src + '/admin');

    }, 1000);        

    });

    // this will change the iframe src and load the page, triggering the iframe load event
    $('iframe[name="content"]').attr('src', src);

  });

});

【讨论】:

  • 哦,对不起,你想然后重定向到管理员,我会改一下代码,抱歉现在有点忙
  • 好的,基本逻辑就在那里,但你需要改进它。希望这会有所帮助:)
  • 绝对被您试图提供帮助的努力所打倒...这是一个非常新手的问题...在加载初始源后,我在哪里指定框架重定向到的源?对不起...
  • 刚看到你的 cmets,是的,重定向会很棒,谢谢伙计,当你有时间的时候 :)
  • 好的,我可以评论代码,等待几秒钟,但现在你会注意到在内部 setTimout 我只是连接到 src 一个新字符串,你会看到 src + '/admin ' ?
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
相关资源
最近更新 更多