我想使用一个Javascript函数在名为“cake”的iframe中加载一个网页。我该怎么做?
<html>
<head>
</head>
<body>
<button onclick=[function]>Load page in iframe</a>
</body>
</html>
我想使用一个Javascript函数在名为“cake”的iframe中加载一个网页。我该怎么做?
<html>
<head>
</head>
<body>
<button onclick=[function]>Load page in iframe</a>
</body>
</html>
function changeUrl() {
var site = "https://www.google.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>
<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>
iframe
,但通常可以使用document.frames['cake'].location.href = what_ever;
。或者为你的iframe
指定一个id
并使用document.getElementById('iframe_id').src ='what_ever';
。此外,建议熟悉你正在使用的元素。 - Teemu