您的位置:首页 > Web前端 > JavaScript

支持FireFox的JavaScript表格操作函数

2008-04-10 08:35 597 查看
想在表格中添加一行数据,以前用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);

}

  说来也没有什么新鲜的玩意儿,其实关键还是在于取得表格对象。具体的用法就不详细说了,自己举一反三吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: