怎样通过js改变表格中某个td的内容?

JavaScript014

怎样通过js改变表格中某个td的内容?,第1张

js动态改变元素内容分两步。

选择器确定好这个元素,不管是用document.getElementById("id")还是其他的都可以.

第二、确定好元素了,只要知道某种标签对应值是哪个属性控制的。只需要改一下对应属性值就可以了。拿你说的td元素来说,里面的内容对应的属性是innerText()。

所以可以使用document.getElementById("td的id值").innerText("想要修改为的值")即可。

希望能够帮助到您,谢谢。

1,Javascript操作table,tr,td ,代码如下:function messageSort() { --函数名var message=document.getElementById("message").value--添加的内容(下面有对应的html)if(name == "" ) return--如果添加为空,返回var row = document.createElement("tr")//创建tr的row.setAttribute("id", name)--设置row的属性. var cell = document.createElement("td")//创建tdcell.appendChild(document.createTextNode(name))//td里注入文本row.appendChild(cell)//将TD注入TRvar deleteButton = document.createElement("input")//这部分是添加删除button按钮deleteButton.setAttribute("type", "button")deleteButton.setAttribute("value", "删除")deleteButton.onclick = function () { deleteSort(name)}cell = document.createElement("td")cell.appendChild(deleteButton)//注入按钮row.appendChild(cell)//将TD注入TRdocument.getElementById("sortList").appendChild(row)//将TR注入到相应地方(sortList可以看下面html)var cell5 = document.createElement("td")cell.style.background="#ffffff"//背景颜色设置row1.style.color="#ffffff"//字体颜色设置cell5.style.display = "none" //ie不支持setAttribute("style", "display:none")// <td style="display:none" >dd</td>直接写TD是这样..cell5.appendChild(document.createTextNode(zdid))row.appendChild(cell5)}// 删除内容function deleteSort(id) {//这个函数为上面的删除button调用的var rowToDelete = document.getElementById(id)var sortList = document.getElementById("sortList")sortList.removeChild(rowToDelete)}</script></head><body><table border="0" cellspacing="0" width="400" bgcolor="#f5efe7"><tr><td height="20">增加内容:</td><td><input id="message" type="text"></td><td><a href="javascript:messageSort()">添加</a></td></tr></table><table border="1" width="400"><tr><td height="20" align="center">内容:</td><td>操作</td></tr><tbody id="sortList"></tbody></table></body>2,一般情况下定义一个效果良好的表格采用下面的属性定义方式代码:<table cellSpacing="0" cellPadding="0" border='1' bordercolor="black" style='border-collapse:collapsetable-layout: fixed'></table>当某个td中没有内容或者没有可见元素时,td的border也会消失。解决方案就是给table添加样式border-collapse:collapse 代码段:.text-overflow{ display:block/*内联对象需加*/ width:31emword-break:keep-all/* 不换行 */ white-space:nowrap/* 不换行 */ overflow:hidden/* 内容超出宽度时隐藏超出部分的内容 */ text-overflow:ellipsis/* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden一起使用。*/ } 3,但对于表格table来讲是有些不同,代码段:table{ width:30emtable-layout:fixed/* 只有定义了表格的布局算法为fixed,下面td的定义才能起作用。 */ } td{ width:100%word-break:keep-all/* 不换行 */ white-space:nowrap/* 不换行 */ overflow:hidden/* 内容超出宽度时隐藏超出部分的内容 */ text-overflow:ellipsis/* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden一起使用。*/ }

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

<TABLE id="aaa">

<TR>

<TD>s1</TD>

<TD>s2</TD>

</TR>

<TR>

<TD>s3</TD>

<TD>s4</TD>

</TR>

</TABLE>

一个表格如上,如何用javascript把内容为s3的td的ID设置为ss3

????

解析:

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

<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 class="page_speeder_1153071515" 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>