gpt4 book ai didi

确定表格宽度/高度的Javascript

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:03 25 4
gpt4 key购买 nike

我有一些 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>

有人看到我做错了什么吗?

最佳答案

试试这个可能有用

    var width = document.getElementById('container').style.offsetWidth;
var height = document.getElementById('container').style.offsetheight;

关于确定表格宽度/高度的Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com