使用Javascript在iframe中加载html页面

Using Javascript to load an html page inside an iframe

本文关键字:加载 html 页面 iframe Javascript 使用      更新时间:2023-09-26

我正在尝试让每个选项在帧中加载不同的页面

<!DOCTYPE html>
<html>
<head>
<script>
function selectCountry()
{
var mylist=document.getElementById("country");
document.getElementById("frame").src=mylist.options[mylist.selectedIndex].html;
}
</script>
</head>
<form action="">
<select id = "country" onchange="selectCountry()">
<option value="america">America</option>
<option value="austria">Austria</option>
<option value="belgium">Belgium</option>
<option value="bosnia">Bosnia and Herzegovina</option>
<option value="croatia">Croatia</option>
<option value="estonia">Estonia</option>
<option value="france">France</option>
<option value="germany">Germany</option>
<option value="greece">Greece</option>
<option value="hungary">Hungary</option>
<option value="italy">Italy</option>
<option value="netherlands">Netherlands</option>
<option value="russia">Russia</option>
<option value="serbia">Serbia</option>
<option value="sweden">Sweden</option>
<option value="switzerland">Switzerland</option>
<option value="uk">UK</option>
</select>
</form>
<iframe id = "frame"></iframe>
</body>
</html>

但是该页面没有加载到iframe中,尽管该页面与上面的index.html位于同一文件夹中,apache还是会给出一条未找到对象的消息。我试着换了条线:

document.getElementById("frame").src=mylist.options[mylist.selectedIndex].html;

至:

document.getElementById("frame").src=mylist.options[mylist.selectedIndex] + ".html";

但没有快乐。请帮忙。

您可以这样做。

function selectCountry(){
 var mylist=document.getElementById("country");
 document.getElementById("frame").src=mylist.value+'.html';
}

mylist.options[mylist.selectedIndex].value

缺少value