用于确定表格宽度/高度的 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11912673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript to determine table width/height
提问by cream
I've got some JavaScript I'm trying to use to define my table width/height to make it fluid across different resolutions. It seems to work fine for the width but I can't get it to work for the height. It is setting the correct number into the correct variable, it's just not setting the tables height. Here's what I got..
我有一些 JavaScript,我试图用它来定义我的表格宽度/高度,以使其在不同的分辨率下流畅。它似乎适用于宽度,但我无法让它适用于高度。它正在将正确的数字设置为正确的变量,而不是设置表格高度。这是我得到的..
<html>
<head>
<script type="text/javascript">
var viewportwidth;
var viewportheight;
var tablewidth;
var tableheight;
if (typeof window.innerWidth != 'undefined'){ //set's viewport width/height into respective variables
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0){
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}else{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
function asdf(){
tablewidth = Math.round(viewportwidth/100*70); //sets table width to 70% of viewport width
tableheight = Math.round(tablewidth/100*74); //sets table height to 74% of table width, this number is ambigous
document.getElementById('poop').innerHTML = tableheight+', '+tablewidth+', viewport width is '+viewportwidth+'x'+viewportheight;
document.getElementById('container').width = tablewidth;
document.getElementById('container').height = tableheight;
}
</script>
</head>
<body onload="asdf();">
<span id="poop"></span>
<table id="container" cellpadding="0" border="1" cellspacing="0" width="" height="">
<tr><td colspan="5"></td></tr>
<tr>
<td width="38%"></td>
<td width="12%"></td>
<td width="21%"></td>
<td width="14%"></td>
<td width="15%"></td>
</tr>
</table>
</body>
</html>
Anyone see what I'm doing wrong?
有人看到我做错了什么吗?
采纳答案by sejal patel
Try this may this work
试试这可能会奏效
var width = document.getElementById('container').style.offsetWidth;
var height = document.getElementById('container').style.offsetheight;
回答by Blender
Why don't you just use CSS?
为什么不直接使用 CSS?
#container {
width: 70%;
height: 74%;
}
回答by cream
As it turns out, there was nothing wrong with the Javascript. I didn't realize you can't put the height attribute in the <table>
element, it must go in the <td>
element.
I confirm, in PhoneGap, a simple table with height=100% doesn't size to the screen dimension, in HTML, in CSS, inside or outside of a DIV, etc...
My solution inspire by this post: I have compute the height of the table via the body height and distribute in pixel to each of my table the dimension.
/* this code is documented before, works fine and is here just to complete the presentation */
事实证明,Javascript 没有任何问题。我没有意识到你不能把高度属性放在<table>
元素中,它必须放在<td>
元素中。
我确认,在 PhoneGap 中,一个高度为 100% 的简单表格不适合屏幕尺寸、HTML、CSS、DIV 内部或外部等......
我的解决方案受到这篇文章的启发:我有计算桌子的高度通过身体高度并以像素为单位分配到我的每个桌子的尺寸。
/* 这段代码之前已经记录在案,工作正常,在这里只是为了完成演示 */
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
/* this is a Topcoat navigation bar and I get its height via offsetHeight of the surrounding <div> */
var hTopBar = document.getElementById("pgTopBar").offsetHeight;
var sH = Math.floor((height - hTopBar) / 3);
document.getElementById("td1").height = sH;
document.getElementById("td2").height = sH;
document.getElementById("td3").height = sH;
document.getElementById("td4").height = sH;
document.getElementById("td5").height = sH;
document.getElementById("td6").height = sH;
In my case, 2 cells per row, 3 rows in the table, so the height is 33% of the required height of the table.
在我的情况下,每行 2 个单元格,表格中有 3 行,因此高度是表格所需高度的 33%。
回答by code-jaff
The working javascript code is
工作的javascript代码是
//With small change, which you are missing
document.getElementById('container').style.width = tablewidth+"px";
document.getElementById('container').style.height = tableheight+"px";
You can check fiddle demo
您可以查看小提琴演示