使用JavaScript设置TD的宽度

时间:2014-09-06 18:30:32

标签: javascript html-table

我正在尝试根据窗口宽度减去像素数量来设置TD的宽度,并使其响应。我特意尝试使用它来制作一个与实时流和聊天窗口大小相同的页面,但它似乎不适用于宽度减去我设置的像素数量。

以下是代码:

<table id="table_big"> 
    <tr>
        <td id="player_table">Content is not important just text</td>
        <td id="chat_table">Content is not important just text</td>
    </tr>
</table>

<script language="javascript" type="text/javascript">
    window.addEventListener("resize", resize);
    function resize()
    {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth ||                             document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        document.getElementById("chat").height = myHeight* 0.78 + 'px';
        document.getElementById("width").width = myWidth ;
        document.getElementById("table_big").height = myHeight + 'px';
        document.getElementById("chat_table").width = '340px';
        document.getElementById("player_table").width = myWidth - '340px';
    }
    resize();
</script>

Shashank修正。行document.getElementById("width").width = myWidth;返回null,我也从所有这些中删除了px

0 个答案:

没有答案