使用下拉菜单加载 iframe 的 src,然后进行更改



>我有以下代码

<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秒。

因此,如果选择示例站点 2,则用户单击"加载站点",加载 http://www.example2.com,然后将 iframe 重定向到 http://www.example2.com/admin。

谢谢

只是想帮忙!这是一个解决方案(您可以使用代码 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);
  });
});

相关内容

最新更新