2015-05-07 21 views
0

这里是我的HTML源代码:JS:iframe2如何更改iframe1的src?

<body> 
<iframe id='iframe1' src="http://site1.com/myScript.html"></iframe> 
<iframe id='iframe2' src="http://site2.com"></iframe> 
</body> 

我如何(在myScript.html页)改变IFRAME的src? (或导航到site3.com)

myScript.html;

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 
<script> 
function navigateIFrame2() { 
// code to change the url or navigate iframe2 
} 
</script> 

</body> 
</html> 
+0

你不能。不同域中的iframe不能访问对方。 – Barmar

回答

0
$('#iframe2', window.parent.document).attr("src" "new url"); 
+0

这个答案出现在低质量的审查队列中,大概是因为你没有解释代码。如果你解释它(在你的答案中),你更有可能得到更多的赞扬 - 而提问者更可能学到一些东西! –

+0

不起作用... – yarek

+0

是同一网站上的iframe吗?我错过了src和新网址之间的逗号。 – berblexer