这两天在调测系统的兼容性时遇到一个设置table高度的问题,页面功能很简单,就是页面中央一个文件,文件下方有一个保存按钮,文件内可以点击“添加项目按钮”来在该文件内增加表格数(这就意味着文件的高度随时可能发生变化)。现在的问题是IE显示正常,但是chrome中保存按钮框始终出现在文件的中央(真想偷懒设置一个定死的高度,呵呵,1365px,不要问我怎么知道,我没有这么 想过~_~)。
最简单的动态设置table高度很容易实现,动态获取当前文件的高度然后赋值给保存按钮就可以了。
function com_onresize(){
//clientHeight的值由DIV内容的实际高度和CSS中的padding值决定,
//而offsetHeight的值由DIV内容的实际高度,CSS中的padding值,scrollbar的高度和DIV的border值决定;
//至于CSS中的margin值,则不会影响 clientHeight和offsetHeight的值
var contentsHeight = document.body.clientHeight;
var buttonsHeight = document.getElementById( "buttons" ).offsetHeight;
var head1Height = document.getElementById( "head1" ).offsetHeight;
var head2Height = document.getElementById( "head2" ).offsetHeight;
var h = contentsHeight - buttonsHeight - head1Height - head2Height - ;
if(h < ){
return;
}
//设置table的行高
document.getElementById( "TableContainer1" ).style.height = h/ + 'px';
document.getElementById( "TableContainer2" ).style.height = h/ + 'px';
}
function com_sbs_pagesize(){
//window.screen.availWidth 返回当前屏幕宽度(空白空间)
//window.screen.availHeight 返回当前屏幕高度(空白空间)
//window.screen.width 返回当前屏幕宽度(分辨率值)
//window.screen.height 返回当前屏幕高度(分辨率值)
//window.document.body.offsetHeight; 返回当前网页高度
//window.document.body.offsetWidth; 返回当前网页宽度
var screenHeight = window.screen.height;
var availHeight = window.screen.availHeight;
var index = document.getElementById( "TableContainer1" ).children[].rows.length;
var buttonsHeight = document.getElementById( "buttons" ).offsetHeight;
var head1Height = document.getElementById( "head1" ).offsetHeight;
var head2Height = document.getElementById( "head2" ).offsetHeight;
var mainH = buttonsHeight + head1Height +head2Height + ;
while ((availHeight-mainH) < ( + + *index)*){
index = index - ;
}
var tableHeight = + + *index;
document.getElementById( "TableContainer1" ).style.height = tableHeight + 'px';
document.getElementById( "TableContainer2" ).style.height = tableHeight + 'px';
//window.resizeTo(width,height)方法用于把窗口大小调整为指定的宽度和高度
window.resizeTo(document.body.offsetWidth,mainH + tableHeight*);
}
但是问题是我来改这个bug的时候发现这个文件被分成了多个div多个table,前台页面写的好乱!而且当我去尝试使用JS脚本给TR的style.height属性赋值,这个高度设定并不能表现出来,上网搜了一下,别人是这样回答的:
确实在Chrome,使用JS脚本给TR的style.height属性赋值,不能表现出来。愚以为,TR元素只是容纳定位TD元素的容器,它本身并没有外观。给TR的height赋值,是把其height属性传递给了其中的TD。使用JS赋值的时候,IE和FF都把值传递给了TD,Chrome要么是没有给TD传递这个值,要么传递了没有表现出来。我认为前者可能性比较大。
然后我又查了一下W3C里面
<tr>
和
<td>
的属性
<tr>
标签的可选属性
<td>
标签的可选属性
问题就显而易见了。
附录:
网页可见区域宽: document.body.clientWidth;
网页可见区域高: document.body.clientHeight;
网页可见区域宽: document.body.offsetWidth (包括边线的宽);
网页可见区域高: document.body.offsetHeight (包括边线的宽);
网页正文全文宽: document.body.scrollWidth;
网页正文全文高: document.body.scrollHeight;
网页被卷去的高: document.body.scrollTop;
网页被卷去的左: document.body.scrollLeft;
网页正文部分上: window.screenTop;
网页正文部分左: window.screenLeft;
屏幕分辨率的高: window.screen.height;
屏幕分辨率的宽: window.screen.width;
屏幕可用工作区高度: window.screen.availHeight;
屏幕可用工作区宽度:window.screen.availWidth;
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
document.documentElement.scrollTop 垂直方向滚动的值
offsetParent.scrollTop 表示上层定位容器滚动了多少高度
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量;
要获取当前页面的滚动条纵坐标位置,用:
document.documentElement.scrollTop;而不是:document.body.scrollTop;
documentElement 对应的是 html 标签,而 body 对应的是 body 标签
document.body.scrollWidth很好理解, 就页面不带右边滚动条的长度。
document.body.scrollHeight指页面的总高度,当前一屏显示高度再加上纵向滚动条滚动到底的高度
document.documentElement.scrollTop和document.body.scrollTop涉及到chrome浏览器的兼容性问题:
当页面加入DTD标示后 document.documentElement.scrollTop的值在IE和FF下正常,但document.body.scrollTop一直为0。在Chrome下就反过来了;
最后编辑时间:2016-03-22