支持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);
}
说来也没有什么新鲜的玩意儿,其实关键还是在于取得表格对象。具体的用法就不详细说了,自己举一反三吧。
第一个函数,用来确定对象:
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);
}
说来也没有什么新鲜的玩意儿,其实关键还是在于取得表格对象。具体的用法就不详细说了,自己举一反三吧。
相关文章推荐
- JavaScript动态添加删除表格行(支持FireFox)
- JavaScript实现二级、多级(N级)联动下拉列表框更新版,支持IE6,FireFox,函数和类两种调用方式,支持到N级,非常通用。
- javascript中支持firefox的xml读取操作实例
- 支持firefox javascript 添加删除表格
- JavaScript实现二级、多级(N级)联动下拉列表框更新版,支持IE6,FireFox,函数和类两种调用方式,支持到N级,非常通用
- JavaScript动态添加删除表格行(支持FireFox)
- JavaScript动态添加删除表格行(支持FireFox)
- javascript 支持ie和firefox杰奇翻页函数
- JavaScript实现二级、多级(N级)联动下拉列表框更新版,支持IE6,FireFox,函数和类两种调用方式,支持到N级,非常通用。
- JavaScript实现二级、多级(N级)联动下拉列表框更新版,支持IE6,FireFox,函数和类两种调用方式,支持到N级,非常通用。
- JavaScript动态添加删除表格行(支持FireFox)(转)
- JavaScript动态添加删除表格行(支持FireFox)
- JavaScript 表格对象和操作
- 按键盘方向键翻页跳转的javascript代码(支持ie,firefox)
- 用 JavaScript 来操作字符串的函数
- JavaScript 进度条实现代码(Firefox等相似浏览器下不支持)
- 用JavaScript对表格操作
- JavaScript的操作数组的函数
- javascript身份证号码验证函数支持带x
- 发布一个 JavaScript 右击菜单 支持 ie678,Firefox,Safari