opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用。 opener 属性非常有用,创建的窗口可以引用创建它的窗口所定义的属性和函数。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Brower Object Model 浏览器对象模型 Window对象</title>
<script>
//点击按钮,设置要跳转的窗口属性
function jump() {
window.open('user.html', '', 'width=500,height=300,left=300,top=100');
}
</script>
</head>
<body>
编号:<input type="text" name="num" id="numId" /><br />
姓名:<input type="text" name="name" id="nameId" /><br />
<input type="button" value="选择" onclick="jump()" />
</body>
</html>
user.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
tr,
td {
text-align: center;
}
</style>
<script>
//点击按钮,获取编号和姓名的值,将值赋给创建当前窗口的父窗口中相应的对象。
//window.opener 返回的是创建当前窗口的那个父窗口的引用。
function run(num, name) {
var w = window.opener;
w.document.getElementById('numId').value = num;
w.document.getElementById('nameId').value = name;
window.close();
}
</script>
</head>
<body>
<table border="1" width="96%">
<tr>
<th>操作</th>
<th>编号</th>
<th>姓名</th>
</tr>
<tr>
<td><input type="button" value="选择" onclick="run('001','张三')" /></td>
<td>001</td>
<td>张三</td>
</tr>
<tr>
<td><input type="button" value="选择" onclick="run('002','李四')" /></td>
<td>002</td>
<td>李四</td>
</tr>
<tr>
<td><input type="button" value="选择" onclick="run('003','王五')" /></td>
<td>003</td>
<td>王五</td>
</tr>
</table>
</body>
</html>