把要增加的行设置成隐藏,display = 'none' 当要显示时 display = ''可以达到同样效果
解决方案 »
- 如何判断网页文档加载完毕?
- ext 的这个写法哪错了
- *********************************字符串和数字 没有原型链?????
- 谁能把新浪或者网易邮箱的弹出窗效果代码给抠下来,我抠了一个早上没成功
- 怎么要web中调用一个本地的java程序,需要做成插件吗?
- 如何指定input要上传的文件?
- 在不修改firefox的安全策略前提下如何能预览通过input type="file"获得的本地图片?
- 请问如何将SELECT下拉框定住(不是DISABLED)
- 请问有人知道有方法屏蔽IE上的“后退”键吗?
- 关于页面html代码有变更后选中状态保留问题
- 请教一个有关变量的问题
- 写了一个超简单的js,可是不知道怎么调用
像这样
<script>
function insert(id){
var otr = document.getElementById(id), ntr = otr.nextSibling, tr = document.createElement('tr'), td = document.createElement('td');
td.innerHTML = '<input type="text" name="user" />';
tr.appendChild(td);
ntr ? ntr.parentNode.insertBefore(tr, ntr) : otr.parentNode.appendChild(tr);
}
</script>
<table>
<tr id="a"><td>a</td></tr>
<tr><td>b</td></tr>
</table>
<input type="button" onclick="insert('a')" value="插入" />
var oTr = oTable.insertRow(-1);//在最后一行后面插入
var oCell = oTr.insertCell(); //插入表格
var newRow = table.insertRow(1)
参考:
insertRow Method Internet Development Index --------------------------------------------------------------------------------Creates a new row (tr) in the table, and adds the row to the rows collection. SyntaxoTR = object.insertRow( [iIndex])
ParametersiIndex Optional. Integer that specifies where to insert the row in the rows collection. The default value is -1, which appends the new row to the end of the rows collection. Return ValueReturns the tr element object if successful, or null otherwise. ResIf you insert a row in a tFoot, tBody, or tHead, you also need to add the row to the rows collection for the table. If you insert a row in the table, you also need to add the row to the rows collection for the tBody. If you specify an index, the index should be relative to the rows collection for the element that first contains the tr. For example, if you call this method for a tBody, you must specify an index value relative to the rows collection that is on the tBody, not the table. The preferred technique for inserting a row is to add the row at the end of the rows collection. It is faster to add a row at the end of a table than somewhere in the middle. To add a row at the end of the collection, specify the -1 value, or the length of the rows collection minus 1.ExampleThis example uses the insertRow method to add a row to the table. myNewRow = document.all.myTable.insertRow()
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
var oldRow;
//选中行
function changeTrColor(row){
if(oldRow){
oldRow.style.backgroundColor="";
}
row.style.backgroundColor="red";
oldRow=row;
}
//在选中行之前插入新行
function addRow(){
if(!oldRow){
alert("请选择行!");
return false;
}
var table=document.getElementById("tab");
var n=oldRow.rowIndex;
var row=table.insertRow(n);
addEvent(row,"click",function(){changeTrColor(row);});
var cell=row.insertCell();
cell.appendChild(document.createTextNode("new row"+n));
}
//添加事件
function addEvent(object,eventType,eventHandler){
if(object.addEventListener){//2级DOM
object.addEventListener(eventType,eventHandler,true);
}else if(document.attachEvent){//IE5+
object.attachEvent("on"+eventType,eventHandler);
}else{//IE4
object["on"+eventType]=eventHandler;
}
}
</script>
<BODY>
<table id="tab" border="1">
<tr onclick="changeTrColor(this);">
<td>dsaf</td>
</tr>
<tr onclick="changeTrColor(this);">
<td>dsaf</td>
</tr>
<tr onclick="changeTrColor(this);">
<td>dsaf</td>
</tr>
</table>
<input type="button" value="new row" onclick="addRow();">
</BODY>
</HTML>
var tr=ob.insertRow(1);
这样就OK啦