我有一个显示网址预览的iframe:
<iframe style="width:100%;height:700px;" id="myframe" src="www.google.com"></iframe>
但是,我希望有一个网址列表,并且能够移动&#39;通过它们,这意味着每次按空格键时,它都会显示iframe中的下一个网址:
<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>
有谁知道如何将此添加到此代码中?谢谢
答案 0 :(得分:1)
您可以更改迭代网站数组的iframe src属性,如下所示:
var websites = ["http://www.google.com", "http://www.bing.com", "http://www.yahoo.com", "http://www.youtube.com"];
var counter = 1;
$(window).keypress(function (e) {
if (e.keyCode === 0 || e.keyCode === 32) {
$('#myframe').attr('src', websites[counter]);
if(counter == websites.length -1) counter = 0;
else counter += 1;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe style="width:100%;height:700px;" id="myframe" src="http://www.google.com"></iframe>