2015-09-25 39 views
1

我試圖使用Javascript函數在名爲「cake」的iframe中加載網頁。我怎樣才能做到這一點?使用JavaScript函數在iFrame中加載頁面

<html> 
    <head> 
    </head> 
    <body> 
    <button onclick=[function]>Load page in iframe</a> 
    </body> 
</html> 
+0

似乎有不爲'在iframe'名爲 「蛋糕」 的HTML,但是一般來說,'document.frames ['cake']。location.href = what_ever;'。或者爲'iframe'提供'id'並且說'document.getElementById('iframe_id')。src ='what_ever';'。也建議熟悉[你正在使用的元素](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe)... – Teemu

+0

我知道,製作一個名爲「蛋糕」的iframe是我想要(打算)製作的。 – MistaBoodadee

+0

你在問如何訪問一個iframe,而不是如何創建一個... – Teemu

回答

4

function changeUrl() { 
 
    var site = "http://www.w3schools.com"; 
 
    document.getElementsByName('iFrameName')[0].src = site; 
 
}
<html> 
 
     <head> 
 
     </head> 
 
     <body> 
 
      <button onclick="changeUrl()">Load page in iframe</button> 
 
      <iframe name="iFrameName"></iframe> 
 
     </body> 
 
    </html>

+0

OP有一個名稱,而不是ID? – Teemu

+0

修改了代碼。希望它幫助.. :) –

+0

Saaaaweet!謝啦。傳入我的實施你貢獻什麼... – MistaBoodadee

1

這工作:

<html> 
 

 
<head> 
 

 

 
<script type="text/javascript"> 
 

 
function changeUrl() { 
 
    var site = "1.wav"; 
 
    document.getElementsByName('cake')[0].src = site; 
 
} 
 

 

 
</script> 
 

 

 
</head> 
 

 
<body> 
 

 

 
<center> 
 

 
<iframe src="http://www.w3schools.com" height=400 width=400 frameborder=0 name = "cake" style =""></></iframe> 
 

 
<br> 
 

 
<br> 
 

 
<button onclick="changeUrl()">Load page in iframe</button> 
 

 
</center> 
 

 

 
</body> 
 

 
</html>

相關問題