在特定页面上,用户会按下一个按钮,但在实际处理之前,我需要偶尔向用户展示一个选项列表以选择适当的选项,并使用该选择来能够继续处理。因此,基本上我需要显示一个弹出窗口,其中包含可用选项的
以下是一个简单的解决方案,可以让您从打开的窗口中获取值。您需要将JavaScript代码注入到打开的窗口中,并使用window.opener
与父窗口进行交互:
HTML
<input id="value" />
<button onclick="openWindow();">Open</button>
JavaScript
function openWindow() {
var i, l, options = [{
value: 'first',
text: 'First'
}, {
value: 'second',
text: 'Second'
}],
newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
newWindow.document.write("<select onchange='window.opener.setValue(this.value);'>");
for(i=0,l=options.length; i<l; i++) {
newWindow.document.write("<option value='"+options[i].value+"'>");
newWindow.document.write(options[i].text);
newWindow.document.write("</option>");
}
newWindow.document.write("</select>");
}
function setValue(value) {
document.getElementById('value').value = value;
}
document.getElementById('value').value = value;
。所以我可以从新窗口访问第一个窗口的元素吗?还有一种方法可以使其模态吗?即用户在关闭第二个窗口之前无法切换到第一个窗口吗? - Cratyluswindow.opener.fn()
(其中 fn
是在全局范围内定义的某个函数)访问父窗口的脚本,甚至是 DOM 元素,例如 window.opener.document.getElementById('value') = this.value
。为了模拟模态模式,您可以创建一个覆盖整个父窗口区域并防止事件传播的 div
(将其放置在 body 的最后一个子元素中),一旦您结束子窗口 - 就将其删除以恢复默认行为。 - Vadimdiv
模拟模态模式 http://jsbin.com/uqamiz/4/edit - Vadim#shim {
opacity: .75;
filter: alpha(opacity=75);
-ms-filter: "alpha(opacity=75)";
-khtml-opacity: .75;
-moz-opacity: .75;
background: #B8B8B8;
position: absolute;
left: 0px;
top: 0px;
height: 100%;
width: 100%;
z-index:990
}
#msgbx {
position: absolute;
left: 50%;
top: 50%;
height: 150px;
width: 350px;
margin-top: -75px;
margin-left: -175px;
background: #fff;
border: 1px solid #ccc;
box-shadow: 3px 3px 7px #777;
-webkit-box-shadow: 3px 3px 7px #777;
-moz-border-radius: 22px;
-webkit-border-radius: 22px;
z-index:999
}
HTML
<div id="shim"></div>
<div id="msgbx">inject list markup here</div>
document.getElementById('shim').style.display=document.getElementById('msgbx').style.display ="block";
To Hide
document.getElementById('shim').style.display=document.getElementById('msgbx').style.display ="none";
z-index
,但你没有在任何地方包含它... - Ian
modalDialog
吗?是的,目前暂时不考虑使用JQuery,因为我不熟悉它。但我计划很快去了解它。 - Cratylus