用于确定表格宽度/高度的Javascript [英] Javascript to determine table width/height

查看:107
本文介绍了用于确定表格宽度/高度的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JavaScript我试图用来定义我的表格宽度/高度,以使其在不同的分辨率下流畅。它似乎对宽度工作正常,但我无法让它在高度工作。它将正确的数字设置为正确的变量,它只是没有设置表格高度。这就是我得到的......

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..

<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?

推荐答案

尝试这可能是这项工作

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

这篇关于用于确定表格宽度/高度的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆