Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在iframe中移动不同的URL?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在iframe中移动不同的URL?

Javascript 如何在iframe中移动不同的URL?,javascript,jquery,Javascript,Jquery,我有一个iframe,显示URL的预览: <iframe style="width:100%;height:700px;" id="myframe" src="www.google.com"></iframe> 但是,我希望有一个URL列表,并且能够在其中“移动”,这意味着每次我按下空格键时,它都会在iframe中显示下一个URL: <script> var websites = ["www.google.com", "www.bing.com

我有一个iframe,显示URL的预览:

<iframe style="width:100%;height:700px;" id="myframe" src="www.google.com"></iframe>

但是,我希望有一个URL列表,并且能够在其中“移动”,这意味着每次我按下空格键时,它都会在iframe中显示下一个URL:

<script>

    var websites = ["www.google.com", "www.bing.com", "www.yahoo.com", "www.youtube.com"];

    //Pressed spacebar
    $(window).keypress(function (e) {
        if (e.keyCode === 0 || e.keyCode === 32) {

        //Show next URL in iframe

        }
    })
</script>

var网站=[“www.google.com”、“www.bing.com”、“www.yahoo.com”、“www.youtube.com”];
//按空格键
$(窗口)。按键(功能(e){
如果(e.keyCode==0 | | e.keyCode==32){
//在iframe中显示下一个URL
}
})

有人知道如何将其添加到此代码中吗?谢谢

您可以在网站数组中迭代更改iframe src属性,如下所示:

var网站=[”http://www.google.com", "http://www.bing.com", "http://www.yahoo.com", "http://www.youtube.com"];
var计数器=1;
$(窗口)。按键(功能(e){
如果(e.keyCode==0 | | e.keyCode==32){
$('#myframe').attr('src',网站[计数器]);
如果(counter==websites.length-1)counter=0;
else计数器+=1;
}
})