有一个父窗口<html> <head>
<title>父窗口</title>
<script>
function jumpSon(){
window.open('son.html','我是子窗口');
}
function addStu(id,name){
var stuTable = document.getElementById("stuTable");
var stuCount = stuTable.rows.length;
var stuRow = stuTable.insertRow(stuCount);
stuRow.id = id;
var stuCell0 = stuRow.insertCell(0);
var stuCell1 = stuRow.insertCell(1);
var stuCell2 = stuRow.insertCell(2);
stuCell0.innerHTML = id;
stuCell1.innerHTML = name;
stuCell2.innerHTML = "<input type='button' value='删除' onclick='deleteStu(" + id + ")'/>"
}
function deleteStu(id){
var stuRow = document.getElementById(id);
var stuTable = document.getElementById("stuTable");
stuTable.deleteRow(stuRow.rowIndex);
}
</script>
</head>
<body>
<input type="button" value="弹出子窗口" onclick="jumpSon()" />
<table id="stuTable" border="1">
<tr>
<td>学生代码</td>
<td>学生姓名</td>
<td>操作</td>
</tr>
</table>
</body>
</html>点击弹出子窗口按钮会弹出一个子窗口<html> <head>
<script>
function add(){
var stuid = document.getElementById("id").value;
var stuname = document.getElementById("name").value;
var parentWindow = window.opener;
parentWindow.addStu(stuid,stuname);
}
</script>
</head>
<body>
学生代码<input id="id" type="text" />
学生姓名<input id="name" type="text" />
<input type="button" value="增加学生" onclick="add()" />
</body>
</html>
点击子窗口中的增加学生,父窗口的表格会增加一行,学生代码和学生姓名都可以随意填写,增加一行没有问题,但是当学生代码带有字母的时候,点击父窗口中的删除时无法删除这行,而学生代码是纯数字就可以,用火狐提示说stuRow is null,求大神解释这是怎么一回事父子窗口传递数据
<title>父窗口</title>
<script>
function jumpSon(){
window.open('son.html','我是子窗口');
}
function addStu(id,name){
var stuTable = document.getElementById("stuTable");
var stuCount = stuTable.rows.length;
var stuRow = stuTable.insertRow(stuCount);
stuRow.id = id;
var stuCell0 = stuRow.insertCell(0);
var stuCell1 = stuRow.insertCell(1);
var stuCell2 = stuRow.insertCell(2);
stuCell0.innerHTML = id;
stuCell1.innerHTML = name;
stuCell2.innerHTML = "<input type='button' value='删除' onclick='deleteStu(" + id + ")'/>"
}
function deleteStu(id){
var stuRow = document.getElementById(id);
var stuTable = document.getElementById("stuTable");
stuTable.deleteRow(stuRow.rowIndex);
}
</script>
</head>
<body>
<input type="button" value="弹出子窗口" onclick="jumpSon()" />
<table id="stuTable" border="1">
<tr>
<td>学生代码</td>
<td>学生姓名</td>
<td>操作</td>
</tr>
</table>
</body>
</html>点击弹出子窗口按钮会弹出一个子窗口<html> <head>
<script>
function add(){
var stuid = document.getElementById("id").value;
var stuname = document.getElementById("name").value;
var parentWindow = window.opener;
parentWindow.addStu(stuid,stuname);
}
</script>
</head>
<body>
学生代码<input id="id" type="text" />
学生姓名<input id="name" type="text" />
<input type="button" value="增加学生" onclick="add()" />
</body>
</html>
点击子窗口中的增加学生,父窗口的表格会增加一行,学生代码和学生姓名都可以随意填写,增加一行没有问题,但是当学生代码带有字母的时候,点击父窗口中的删除时无法删除这行,而学生代码是纯数字就可以,用火狐提示说stuRow is null,求大神解释这是怎么一回事父子窗口传递数据
stuCell2.innerHTML = '<input type="button" value="删除" onclick="deleteStu(\'' + id + '\')"/>'
比如输入的学生编号是"asd",
<input type="button" value="删除" onclick="deleteStu(asd)"/>
这里的asd是变量的意思而不是你想要的字符串
在是数字的情况下
比如学生编号是222,
<input type="button" value="删除" onclick="deleteStu(222)"/>
这里写222跟"222"效果基本一样。