怎么样在IE中实现窗口间操作下拉选框的选项?
怎么样在IE中实现窗口间操作下拉选框的选项?
在b/s系统中,用户经常需要打开子窗口选中某些项目,并将这些项目插入到父窗口的下拉选框中。本来以为在IE中实现这样子窗口操作父窗口的功能十分简单,但是按常规的做法却是行不通的。在google上搜索了一阵也没有好的解决方案。最后看到国外的一个网页上有以下内容:
Explorer 5.0 problems
When it comes to dynamically generating options and selects, Explorer 5.0 on Windows has quite a few problems:
- Generating options in another frame or window doesn't work. Put the script in the page that contains the select. I have heard, but did not test, that this problem still exists in Explorer 6.0
-
Generating selects and options through the 'pure' W3C DOM (ie. with any
document.createElement()
) crashes Explorer 5.0 . Solved in 5.5
Generate these selects and options throughinnerHTML
instead. - Generating options from a popup window may crash any Explorer Windows.
I have heard, but did not test, that the script below works fine in IE 5.0:
vardoc=select.ownerDocument;
if(!doc)
doc=select.document;
varopt=doc.createElement('OPTION');
opt.value=value;
opt.text=text;
select.options.add(opt,index);
if(!doc)
doc=select.document;
varopt=doc.createElement('OPTION');
opt.value=value;
opt.text=text;
select.options.add(opt,index);
最后得到了启发,从而实现了这个功能,下面所有可能用到的实现方法的代码。但是在这些代码中有些方法是不可行的。最后有一个表格说明了哪些方法不可行,理由是什么?
HTMLPage.htm
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>1st</title>
<scriptlanguage="javascript">
functionAddOpt(text,val)
{
varslct=document.getElementById("Select1");
varop=newOption(text,val);
slct.add(op);
}
</script>
</head>
<body>
<formid="form1"name="form1">
<selectid="Select1"multiple="multiple">
<optionselected="selected"value="1">1</option>
<optionvalue="2">2</option>
<optionvalue="3">3</option>
<optionvalue="4">4</option>
</select>
<br/>
<inputid="showModalDialogWithoutArg"type="button"value="showModalDialogWithoutArg"onclick="window.showModalDialog('HTMLPage2.htm');"/>
<br/>
<inputid="showModalDialogWithArg"type="button"value="showModalDialogWithArg"onclick="window.showModalDialog('HTMLPage2.htm',window);"/>
<br/>
<inputid="showModelessDialogWithoutArg"type="button"value="showModelessDialogWithoutArg"onclick="window.showModelessDialog('HTMLPage2.htm');"/>
<br/>
<inputid="showModelessDialogWithArg"type="button"value="showModalDialogWithArg"onclick="window.showModelessDialog('HTMLPage2.htm',window);"/>
<br/>
<inputid="open"type="button"value="open"onclick="window.open('HTMLPage2.htm');"/>
</form>
</body>
</html>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>1st</title>
<scriptlanguage="javascript">
functionAddOpt(text,val)
{
varslct=document.getElementById("Select1");
varop=newOption(text,val);
slct.add(op);
}
</script>
</head>
<body>
<formid="form1"name="form1">
<selectid="Select1"multiple="multiple">
<optionselected="selected"value="1">1</option>
<optionvalue="2">2</option>
<optionvalue="3">3</option>
<optionvalue="4">4</option>
</select>
<br/>
<inputid="showModalDialogWithoutArg"type="button"value="showModalDialogWithoutArg"onclick="window.showModalDialog('HTMLPage2.htm');"/>
<br/>
<inputid="showModalDialogWithArg"type="button"value="showModalDialogWithArg"onclick="window.showModalDialog('HTMLPage2.htm',window);"/>
<br/>
<inputid="showModelessDialogWithoutArg"type="button"value="showModelessDialogWithoutArg"onclick="window.showModelessDialog('HTMLPage2.htm');"/>
<br/>
<inputid="showModelessDialogWithArg"type="button"value="showModalDialogWithArg"onclick="window.showModelessDialog('HTMLPage2.htm',window);"/>
<br/>
<inputid="open"type="button"value="open"onclick="window.open('HTMLPage2.htm');"/>
</form>
</body>
</html>
HTMLPage2.htm
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2nd</title>
<scriptlanguage="javascript">
functionInsertToParent()
{
varslct=window.parent.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertToOpener()
{
varslct=window.opener.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertToTop()
{
varslct=window.top.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertByParentFun()
{
varwnd=window.parent;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByOpenerFun()
{
varwnd=window.opener;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByTopFun()
{
varwnd=window.top;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByArgFun()
{
varwnd=window.dialogArguments;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertWithArg()
{
varwnd=window.dialogArguments;
vardoc=wnd.document;
varslct=doc.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
</script>
</head>
<body>
<inputid="InsertToParent"type="button"value="InsertToParent"onclick="InsertToParent()"/>
<br/>
<inputid="InsertToOpener"type="button"value="InsertToOpener"onclick="InsertToOpener()"/>
<br/>
<inputid="InsertToTop"type="button"value="InsertToTop"onclick="InsertToTop()"/>
<br/>
<inputid="InsertByParentFun"type="button"value="InsertByParentFun"onclick="InsertByParentFun()"/>
<br/>
<inputid="InsertByOpenerFun"type="button"value="InsertByOpenerFun"onclick="InsertByOpenerFun()"/>
<br/>
<inputid="InsertByTopFun"type="button"value="InsertByTopFun"onclick="InsertByTopFun()"/>
<br/>
<inputid="InsertByArgFun"type="button"value="InsertByArgFun"onclick="InsertByArgFun()"/>
<br/>
<inputid="InsertWithArg"type="button"value="InsertWithArg"onclick="InsertWithArg()"/>
</body>
</html>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2nd</title>
<scriptlanguage="javascript">
functionInsertToParent()
{
varslct=window.parent.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertToOpener()
{
varslct=window.opener.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertToTop()
{
varslct=window.top.document.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
functionInsertByParentFun()
{
varwnd=window.parent;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByOpenerFun()
{
varwnd=window.opener;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByTopFun()
{
varwnd=window.top;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertByArgFun()
{
varwnd=window.dialogArguments;
wnd.AddOpt("2nd窗口","2nd窗口");
}
functionInsertWithArg()
{
varwnd=window.dialogArguments;
vardoc=wnd.document;
varslct=doc.getElementById("Select1");
doc=slct.ownerDocument;
varopt=doc.createElement('OPTION');
opt.value="2nd窗口";
opt.text="2nd窗口";
slct.options.add(opt);
}
</script>
</head>
<body>
<inputid="InsertToParent"type="button"value="InsertToParent"onclick="InsertToParent()"/>
<br/>
<inputid="InsertToOpener"type="button"value="InsertToOpener"onclick="InsertToOpener()"/>
<br/>
<inputid="InsertToTop"type="button"value="InsertToTop"onclick="InsertToTop()"/>
<br/>
<inputid="InsertByParentFun"type="button"value="InsertByParentFun"onclick="InsertByParentFun()"/>
<br/>
<inputid="InsertByOpenerFun"type="button"value="InsertByOpenerFun"onclick="InsertByOpenerFun()"/>
<br/>
<inputid="InsertByTopFun"type="button"value="InsertByTopFun"onclick="InsertByTopFun()"/>
<br/>
<inputid="InsertByArgFun"type="button"value="InsertByArgFun"onclick="InsertByArgFun()"/>
<br/>
<inputid="InsertWithArg"type="button"value="InsertWithArg"onclick="InsertWithArg()"/>
</body>
</html>
方法表格
showModalDialogWithoutArg |
InsertToParent |
不能实现 |
子窗口parent属性为子窗口自身 |
InsertToOpener |
不能实现 |
<font size=" |