javascript 从 iframe 中的变量打开链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15659749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open link from variable in iframe
提问by user2215716
<script language="Javascript" type="text/javascript">
function Next(){
var randp = Math.floor(Math.random()*3);
switch(randp){
case 0:
var websel = "http://w3schools.com";
break;
case 1:
var websel = "http://www.bbc.com/";
break;
case 2:
var websel = "http://en.wikipedia.org/wiki/BBC";
break;
default:
document.write("Error SWITCH(randp) " + randp + " ");
break;
}
}
</script>
<iframe name="iframea" src="http://w3schools.com" seamless="seamless" scrolling="auto" height="560px" width="1080px" align="middle" scale="1.5"></iframe>
<input type="button" value="Next" onClick="Next()">
Sorry for the basic question. What would I need to add to have the next button open a link (the link being a variable from a function) with the target as the iframe. Thanks
对不起,基本问题。我需要添加什么才能让下一个按钮打开一个链接(链接是来自函数的变量),目标是 iframe。谢谢
回答by Xavier S.
You have to change the src of your iframe as following:
您必须按如下方式更改 iframe 的 src:
<script language="Javascript" type="text/javascript">
function Next(){
var randp = Math.floor(Math.random()*3);
switch(randp){
case 0:
var websel = "http://w3schools.com";
break;
case 1:
var websel = "http://www.bbc.com/";
break;
case 2:
var websel = "http://en.wikipedia.org/wiki/BBC";
break;
default:
document.write("Error SWITCH(randp) " + randp + " ");
break;
}
alert(websel);
document.getElementById("iframea").src=websel;
}
</script>
<iframe name="iframea" id="iframea" src="http://w3schools.com" seamless="seamless" scrolling="auto" height="560px" width="1080px" align="middle" scale="1.5"></iframe>
<input type="button" value="Next" onClick="Next()">
By the way, you might look on this website this questionis similar.
顺便说一句,你可能会在这个网站上看到这个问题是相似的。