如何用javascript设置table的td的id

如何用javascript设置table的td的id,第1张

参考一下这段,把代码直接贴到文件即可使用

<style>body,td,div,a,input{font:menuline-height:150%}</style>

<table cellspacing=1 cellpadding=1 border=0 width=100% id=t136><tbody><form method=post enctype="multipart/form-data"><input type=hidden name=deal value=true><input type=hidden name=frmID value=22><input type=hidden name=thisdir value=><input type=hidden name=TID value=><tr style="font-weight: bold" bgcolor=ACD0FD><td colspan=2>呵呵</td></tr><tr bgcolor=EFEFD6><td colspan=2><font color=000080>哈哈</font></td></tr><input type=hidden name=frmShowURL value=payment><input type=hidden name=ID value=><tr bgcolor=F0F0F0><td>xixi</td><td>xixi </td></tr><tr bgcolor=F9F9F9><td>hehe</td><td><input type=text class=border_index name=newdir style='ime-mode:disabled'><font color=000080>hehe</font></td></tr><tr bgcolor=F0F0F0><td>新图片上传</td><td><div>

<input type=button onclick=addNew() value=增加 class=button_index><font color=000080>(按一次增加按钮就可以上传一张图片。)</font></div></td></tr><tbody></table></form><script>

var i=0,arr=new Array('F9F9F9','F0F0F0')

function addNew(){

tr=document.all.t136.insertRow()

tr.style.backgroundColor=arr[i%2]

tr.insertCell().innerText="图片"+(++i)

tr.insertCell().innerHTML='<input type=file name=pic'+i+' class=border_index><a href=javascript:void(0) onclick=del()>删除</a>'

}

function del(){

document.all.t136.deleteRow(window.event.srcElement.parentElement.parentElement.rowIndex)

for(i=0i<document.all.t136.rows.length-5i++){

document.all.t136.rows[i+5].cells[0].innerText="图片"+(i+1)

document.all.t136.rows[i+5].cells[1].children[0].name="pic"+(i+1)

document.all.t136.rows[i+5].style.backgroundColor=arr[i%2]

}

}

</script>

应该是这个方法setAttribute("id","yourName")

例子

<html>

<script language="javascript">

function createTr(){

var tb=document.all.tb

var tr=tb.insertRow()

var td=tr.insertCell()

td.innerHTML="test"

td.setAttribute("id","newTd")

td.value="test"

td.onclick=clickMe

}

function clickMe(){

var td=document.getElementById("newTd").value

alert(td)

}

</script>

<body>

<table border="1" id="tb">

<tr>

<td onclick="createTr()">click Me</td>

</tr>

</table>

</body>

</html>

我汗,你的第一第二步骤只是产生一个table对象,还没有附加进HTML DOM,也就是document对象,所以你通过getElementById("mytable")那不到你的table对象。。。我汗,直接这样不就行了

var tr1 = table.insertRow() 3.//获得table的对象,并插入一行


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11348785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存