首页 新闻 会员 周边

父页面给iframe子页面的 下拉页表框添加一个选项 怎么添加

0
悬赏园豆:10 [已关闭问题] 关闭于 2009-12-09 18:46

 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); 

我用这种方式添加 有错。

用这种方式给本页面的下拉框添加选项 是没有问题的。

wbz009的主页 wbz009 | 初学一级 | 园豆:0
提问于:2009-12-09 14:25
< >
分享
所有回答(2)
0

主页面:

代码
<!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>

测试是可以的~~

Pwd | 园豆:158 (初学一级) | 2009-12-09 15:55
0

访问子页面时要用到一个东西:contentWindow。

比如要访问ID为 f 的iframe中ID为s的select,则父页面的代码应为:

document.getElementById("f").contentWindow.document.getElementById("s")

I,Robot | 园豆:9783 (大侠五级) | 2009-12-09 16:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册