想在表格中添加一行数据,以前用NewRow = TableId.insertRow()和NewCell = NewRow.insertCell(),在IE中没有问题,但在FireFox中却没有结果后来没办法只有放弃了表格,使用div,今天突发奇想,到底 要在网上搜索一下能不能使用JavaScript在FireFox中操作表格,居然还被我找到了,原来问题还是出在表格对象的确定上,呵呵~~
第一个函数,用来确定对象:
JavaScript代码
- function findObj(theObj, theDoc)
- {
- var p, i, foundObj;
- if(!theDoc) theDoc = document;
- if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
- {
- theDoc = parent.frames[theObj.substring(p+1)].document;
- theObj = theObj.substring(0,p);
- }
- if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
- for (i=0; !foundObj && i < theDoc.forms.length; i++)
- foundObj = theDoc.forms[theObj];
- for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
- foundObj = findObj(theObj,theDoc.layers.document);
- if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
- return foundObj;
- }
第二个函数,插入一行:
JavaScript代码
- function AddSignRow(){
- // 获取表格对象
- var signFrame = findObj("TableName",document);
- // 添加行
- var newTR = signFrame.insertRow(signFrame.rows.length);
- newTR.id = "SignItem"; // 设置行的ID
- //添加列0
- var newNameTD=newTR.insertCell(0);
- //添加列内容
- newNameTD.innerHTML = "第一列内容";
- //添加列1
- var newNameTD=newTR.insertCell(1);
- //添加列内容
- newNameTD.innerHTML = "第二列内容";
- }
第三个函数,删除指定的行:
JavaScript代码
- function DeleteSignRow(rowid){
- var signFrame = findObj("TableName",document);
- var signItem = findObj(rowid,document);
- //获取将要删除的行的Index
- var rowIndex = signItem.rowIndex;
- //删除指定Index的行
- signFrame.deleteRow(rowIndex);
- }
说来也没有什么新鲜的玩意儿,其实关键还是在于取得表格对象。具体的用法就不详细说了,自己举一反三吧。