问题在代码的注释中...<script type="text/javascript">
function Puzzle(size){
this.size=size;
this.Init.call(this);
}
Puzzle.prototype={
Init:function(){
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
for(var i=0;i<this.size;i++){
var tr = document.createElement("tr");
for(var j=0;j<this.size;j++){
if((i+1)*(j+1)!=this.size*this.size){
var td = document.createElement("td");
td.innerHTML=i*this.size+j+1;
tr.appendChild(td);
td.onclick=(function(obj){
//这里点击td的时候删除该td,为什么一直提示参数无效?哪里有问题呢?
return function(){
tr.removeChild(obj)
}
})(td)
}
}
tbody.appendChild(tr);
}
var div = document.getElementById("puzzle");
div.appendChild(table);
}
}
window.onload=function(){
var puzzle = new Puzzle(4);
}
</script>
function Puzzle(size){
this.size=size;
this.Init.call(this);
}
Puzzle.prototype={
Init:function(){
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
for(var i=0;i<this.size;i++){
var tr = document.createElement("tr");
for(var j=0;j<this.size;j++){
if((i+1)*(j+1)!=this.size*this.size){
var td = document.createElement("td");
td.innerHTML=i*this.size+j+1;
tr.appendChild(td);
td.onclick=(function(obj){
//这里点击td的时候删除该td,为什么一直提示参数无效?哪里有问题呢?
return function(){
tr.removeChild(obj)
}
})(td)
}
}
tbody.appendChild(tr);
}
var div = document.getElementById("puzzle");
div.appendChild(table);
}
}
window.onload=function(){
var puzzle = new Puzzle(4);
}
</script>
解决方案 »
- 关于document对象的一个问题
- this.f.call(this)和this.f()有什么区别?在f.prototype中使用this,是引用f而不是其原型对象吧?
- 关于输入框的问题
- js 相同id值的button按钮,上传文件至服务器
- 紧急求助!showModalDialog的问题,请教高手!!!
- 如何在javascript中将字符串转化为日期型?急!
- 一个JS问题搞不懂?
- 急!javascript里的对象名称能不能用变量组合啊
- 通过:window.open('','','fullscreen=yes,scroll=no.............,')里面是哪个参数使其 最小化 按钮没有的?
- js代码解读 在线等 很急
- 表单的验证
- js正则表达式达人请进~~问题咨询
return function(){
alert(obj.innerHTML)
tr.removeChild(obj)
}
})(td)
function Puzzle(size){
this.size=size;
this.Init.call(this);
}
Puzzle.prototype={
Init:function(){
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
for(var i=0;i<this.size;i++){
var tr = document.createElement("tr");
for(var j=0;j<this.size;j++){
if((i+1)*(j+1)!=this.size*this.size){
var td = document.createElement("td");
td.innerHTML=i*this.size+j+1;
tr.appendChild(td);
td.onclick=(function(obj,tr){
//这里点击td的时候删除该td,为什么一直提示参数无效?哪里有问题呢?
return function(){
tr.removeChild(obj);
}
})(td,tr);
}
}
tbody.appendChild(tr);
}
var div = document.getElementById("puzzle");
div.appendChild(table);
}
}
window.onload=function(){
var puzzle = new Puzzle(4);
}这样试试~
你创建了一个执行环境,给执行环境中的obj作为td的拷贝,但是tr这里并没有进行拷贝,所以在这个执行环境中tr相当于未定义
td.onclick = function() {
//事件中的this就是指向的绑定了该事件的DOM对象啊,LZ为什么要那样写
this.parentNode.removeChild(this);
}
return function(){
tr.removeChild(obj)
}
})(td)
tr是最后一行,你可以发现你点击删除最后一行是正常的,但是点击前三行的单元格是无法删除的,因为你根本无法从第四行中删除前三行中的单元格,以下是我在你的代码中稍作的修改,可能会帮助你
<script type="text/javascript"> function Puzzle(size)
{
this.size=size;
this.Init.call(this);
}
Puzzle.prototype =
{
Init:function()
{
var table = document.createElement("table");
table.border = "1px";
var tbody = document.createElement("tbody");
table.appendChild(tbody);
for(var i=0;i<this.size;i++)
{
var tr = document.createElement("tr");
tr.id = i;
for(var j=0;j<this.size;j++)
{
if((i+1)*(j+1)<= this.size*this.size)
{
var td = document.createElement("td");
td.innerHTML = i*this.size + j + 1;
tr.appendChild(td);
td.onclick = (function(obj)
{
return function()
{
this.parentNode.removeChild(this);
}
})(td);
}
}
tbody.appendChild(tr);
}
var div = document.getElementById("puzzle");
div.appendChild(table);
}
}
window.onload=function()
{
var puzzle = new Puzzle(4);
}
</script>