【问题标题】:Javascript to determine table width/height确定表格宽度/高度的Javascript
【发布时间】:2012-08-08 09:44:00
【问题描述】:

我尝试使用一些 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>

有人知道我做错了吗?

【问题讨论】:

    标签: javascript html-table width


    【解决方案1】:

    试试这个可能有用

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

    【讨论】:

    • 如果表格没有任何样式直接使用------------>var width = document.getElementById('TableID').offsetWidth;
    【解决方案2】:

    为什么不直接使用 CSS?

    #container {
      width: 70%;
      height: 74%;
    }
    

    【讨论】:

    • 好问题。我知道我这样做似乎是个疯狂的人,但这是有实际原因的。不涉及不必要的细节会更容易。
    • 因为今天每天都在增加一项新技术......实际上我们没有空闲时间来制作大量 css 媒体或导入具有与所有浏览器不兼容的糟糕纵横比代码的条件.. . 至少我确定所有设备都启用了 javascript,即使在移动设备和平板电脑中也是如此。
    【解决方案3】:

    事实证明,Javascript 没有任何问题。我没有意识到您不能将 height 属性放在 &lt;table&gt; 元素中,它必须放在 &lt;td&gt; 元素中。

    我确认,在 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;
    

    在我的例子中,每行 2 个单元格,表格中有 3 行,所以高度是表格所需高度的 33%。

    【讨论】:

      【解决方案4】:

      有效的javascript代码是

      //With small change, which you are missing
      document.getElementById('container').style.width = tablewidth+"px";
      document.getElementById('container').style.height = tableheight+"px";
      

      您可以查看fiddle demo

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-31
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2017-05-11
        相关资源
        最近更新 更多