JS(JavaScript)创建不规则表格 ,请高手帮忙实现一下啊

JavaScript014

JS(JavaScript)创建不规则表格 ,请高手帮忙实现一下啊,第1张

只能大概告诉你JavaScript操作表格的方法:

1) 获得表格对象

var tb = document.getElementById(idOfTable) // idOfTable为表格元素的ID, 也可以用document.createElement动态创建, 然后调用document.body.appendChild方法添加到页面上

2) 向表格中添加行

var row = tb.insertRow(rowIdx) // rowIdx是新增的行在表格中的位置索引, 从0开始

3) 向行中添加单元格

var cell = row.insertCell(cellIdx)// cellIdx是新增单元格在行中的位置索引, 从0开始

使用以上方法就可以添加表格中的行与单元格, 如果要跨行或跨列则分别使用单元格对象rowSpan和cellSpan属性控制即可.

2013/8/23, 把你的代码发来, 我试试看[email protected] .

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一起使用。*/ }