日期:2014-05-16  浏览次数:20335 次

js 采用insertRow和deleteRow来添加和删除表格单元格
function addrow(obj,type){ // 表格添加行
	var rowtypedata = [
						[[1, ''], [1,'<input type="text" class="txt" name="newcatorder[]" value="0" />', 'td25'], [5, '<div><input name="newcat[]" value="新分区名称" size="20" type="text" class="txt" /><a href="javascript:;" class="deleterow" onClick="deleterow(this)">删除</a></div>']],
						[[1, ''], [1,'<input type="text" class="txt" name="neworder[{1}][]" value="0" />', 'td25'], [5, '<div class="board"><input name="newforum[{1}][]" value="新版块名称" size="20" type="text" class="txt" /><a href="javascript:;" class="deleterow" onClick="deleterow(this)">删除</a><select name="newinherited[{1}][]"><option value="">继承指定版块设置</option></select></div>']],
						[[1, ''], [1,'<input type="text" class="txt" name="neworder[{1}][]" value="0" />', 'td25'], [5, '<div class="childboard"><input name="newforum[{1}][]" value="新版块名称" size="20" type="text" class="txt" /><a href="javascript:;" class="deleterow" onClick="deleterow(this)">删除</a>&nbsp;<label><input name="inherited[{1}][]" type="checkbox" class="checkbox" value="1">&nbsp;继承上级版块设置</label></div>']],
					  ]; 
	var table = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
	var row = table.insertRow(obj.parentNode.parentNode.parentNode.rowIndex);
		var typedata = rowtypedata[type];
	for(var i = 0; i <= typedata.length - 1; i++) {
		var cell = row.insertCell(i);
		cell.colSpan = typedata[i][0];
		var tmp = typedata[i][1];
		if(typedata[i][2]) {
			cell.className = typedata[i][2];
		}
		//tmp = tmp.replace(/\{(\d+)\}/g, function($1, $2) {return addrow.arguments[parseInt($2) + 1];});
		cell.innerHTML = tmp;
	    	}
}
function deleterow(obj){//表格删除行 
	var table = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
	var tr = obj.parentNode.parentNode.parentNode;
	table.deleteRow(tr.rowIndex);
}

?