var iframe=document.frames["myFrame"];
var ddl_Category=iframe.document.getElementById("ddl1");
var option=document.createElement("option");
option.value=sCode;
option.text=sName;
//添加选择项
ddl_Category.appendChild(option);
我用这种方式添加 有错。
用这种方式给本页面的下拉框添加选项 是没有问题的。
主页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<frameset cols="200,*">
<frame src="HTMLPage1.htm" id="frame1" name="frame1" frameborder="1" />
<frame src="HTMLPage2.htm" id="frame2" name="frame2" frameborder="0" />
</frameset>
</html>
HTMLPage1.htm里有一个Button及脚本.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<body>
<input type="button" id='btn' value="add" onclick="btn_click()"/>
<script type="text/javascript">
function btn_click(){
var st = window.top.frames.item("frame2").document.getElementById("st");
var op = document.createElement("option");
op.setAttribute("value",3);
op.innerText="3";
st.appendChild(op);
}
</script>
</body>
</html>
HTMLPage2.htm里有一个select
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<body>
<select id="st">
<option value="1">1</option>
<option value="2">2</option>
</select>
</body>
</html>
测试是可以的~~
访问子页面时要用到一个东西:contentWindow。
比如要访问ID为 f 的iframe中ID为s的select,则父页面的代码应为:
document.getElementById("f").contentWindow.document.getElementById("s")