最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript to determine table widthheight - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jan 2, 2017 at 19:48 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 11, 2012 at 6:52 creamcream 1,1295 gold badges16 silver badges26 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 3

Try this may this work

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

Why don't you just use CSS?

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

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 */

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.

The working javascript code is

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

发布评论

评论列表(0)

  1. 暂无评论