你的代码有很多问题。我在这里整理了一个工作演示。
HTML
<button id="clickMe">Click me</button>
JavaScript
window.onload = function() {
function parentFunction() {
alert('testing');
}
window.parentFunction = parentFunction;
var years = [1, 2, 3, 4, 5, 6];
function print() {
var new_win = window.open('', 'name', 'height=400,width=500');
//var cols = this.getCols();
var cols = 2;
var output = "<html><head></head><body><table>";
var cell_count = 1;
for (i = 0; i < years.length; i++) {
if (cell_count === 1) {
output += "<tr>";
}
output += "<td onclick='window.opener.parentFunction();'>" + years[i] + "</td>";
cell_count++;
// end the row if we've generated the expected number of columns
// or if we're at the end of the array
if (cell_count > cols || i === years.length - 1) {
output += "</tr>\n";
cell_count = 1;
}
}
output += "</table></body></html>";
new_win.document.write(output);
}
document.getElementById('clickMe').onclick = print;
};
years
未定义
this.getCols()
未定义
parentFunction
在window
范围内(可能)不可见