JS固定表头及固定左边列(续)-修正IE6/IE7兼容问题

时间:2022-08-25 20:29:43

之前的版本在IE6和IE7中,不支持表头锁定(但可以固定左边列),在这一版本中修正了这个问题,现在全部兼容了。

修改了两处代码。

1. 修改前:

function oFixedTable(id, obj, _cfg){
this.id = id;
this.obj = obj;
this.box = this.obj.parentNode;
this.config = {
fixHead: _cfg.fixHead || true,
rows: _cfg.rows || 1,
cols: _cfg.cols || 0,
background: _cfg.background || '#f1f1f1',
zindex: _cfg.zindex || 9999
};

window.setTimeout(this._fixTable, 100, this);
}
修改后:

function oFixedTable(id, obj, _cfg){
this.id = id;
this.obj = obj;
this.box = this.obj.parentNode;
this.config = {
fixHead: _cfg.fixHead || true,
rows: _cfg.rows || 1,
cols: _cfg.cols || 0,
background: _cfg.background || '#f1f1f1',
zindex: _cfg.zindex || 9999
};

this.ieLowVersion = navigator.userAgent.indexOf('MSIE 6.0') >= 0 || navigator.userAgent.indexOf('MSIE 7.0') >= 0;

window.setTimeout(this._fixTable, 100, this);
}

增加了一句代码,判断当前浏览器是否为IE6或IE7

this.ieLowVersion = navigator.userAgent.indexOf('MSIE 6.0') >= 0 || navigator.userAgent.indexOf('MSIE 7.0') >= 0;

2. 修改前:

oFixedTable.prototype.buildHead = function(){
var _ = this;
var strDivId = _.id + '_div_head';
var strTbId = _.id + '_tb_header';
var div = document.createElement('div');
div.id = strDivId;
div.style.cssText = 'position:absolute;overflow:hidden;z-index:' + (_.config.zindex + 1) + ';';
div.innerHTML = '<table id="' + strTbId + '" cellpadding="0" cellspacing="0" style="background:' + _.config.background + ';"></table>';

_.box.insertBefore(div, _.obj);

_.divHead = div;
_.tbHead = document.getElementById(strTbId);

//判断是否出现纵向滚动条,若出现,高度减去滚动条宽度 16px
var sw = _.obj.offsetHeight > _.box.offsetHeight ? 16 : 0;
_.divHead.style.width = (_.box.offsetWidth - sw) + 'px';

_.tbHead.style.textAlign = _.obj.style.textAlign;
_.tbHead.style.width = _.obj.offsetWidth + 'px';

var hasHead = false;
if(_.config.fixHead && _.obj.tHead != null){
var tHead = _.obj.tHead;
_.tbHead.appendChild(tHead.cloneNode(true));
hasHead = true;
} else {
for(var i=0; i<_.config.rows; i++){
var row = _.obj.rows[i];
if(row != null){
_.tbHead.appendChild(row.cloneNode(true));
hasHead = true;
}
}
}
return hasHead;
};

修改后:

oFixedTable.prototype.buildHead = function(){
var _ = this;
var strDivId = _.id + '_div_head';
var strTbId = _.id + '_tb_header';
var div = document.createElement('div');
div.id = strDivId;
div.style.cssText = 'position:absolute;overflow:hidden;z-index:' + (_.config.zindex + 1) + ';';
div.innerHTML = '<table id="' + strTbId + '" cellpadding="0" cellspacing="0" style="background:' + _.config.background + ';"></table>';

_.box.insertBefore(div, _.obj);

_.divHead = div;
_.tbHead = document.getElementById(strTbId);

//判断是否出现纵向滚动条,若出现,高度减去滚动条宽度 16px
var sw = _.obj.offsetHeight > _.box.offsetHeight ? 16 : 0;
_.divHead.style.width = (_.box.offsetWidth - sw) + 'px';

_.tbHead.style.textAlign = _.obj.style.textAlign;
_.tbHead.style.width = _.obj.offsetWidth + 'px';

var hasHead = false;
if(_.config.fixHead && _.obj.tHead != null){
var tHead = _.obj.tHead;
_.tbHead.appendChild(tHead.cloneNode(true));
hasHead = true;
} else {
if(!_.ieLowVersion){
for(var i=0; i<_.config.rows; i++){
var row = _.obj.rows[i];
if(row != null){
_.tbHead.appendChild(row.cloneNode(true));
hasHead = true;
}
}
} else {
for(var i=0; i<_.config.rows; i++){
var row = _.tbHead.insertRow(_.tbHead.rows.length);
row.style.cssText = _.obj.rows[i].style.cssText;

for(j=0,c=_.obj.rows[i].cells.length; j<c; j++){
var cell = _.obj.rows[i].cells[j];
if(cell != null){
row.appendChild(cell.cloneNode(true));
cell.style.cssText = _.obj.rows[i].cells[j].style.cssText;
hasHead = true;
}
}
}
}
}
return hasHead;
};

在IE6、IE7中,不采用直接复制行,而是创建行,再复制单元格。

至此,兼容全部浏览器了。