首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript宽度时表中的额外空间

使用javascript宽度时表中的额外空间
EN

Stack Overflow用户
提问于 2015-10-10 21:27:03
回答 1查看 60关注 0票数 0

相反,对于每个表,我使用CSS宽度,我使用javascript自动使表的宽度等于单元格内的文本数量。但是,当滚动水平滚动条时,它也会将空白(绿色空间)保留在表的最右侧。是哪种代码使这个空白成为空白,以及删除这个空白的好方法是什么?

参见示例:http://codepen.io/anon/pen/wKqwqR (我的屏幕大小: 1366*768)

HTML:

代码语言:javascript
复制
<div class="table">
  <table>
<tr>
    <th>Number</th>
    <th>INFO----1</th>
    <th>INFO----2</th>      
    <th>Digits</th>
</tr>
<tr>
    <td>1</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>       
    <td>94</td>
</tr>
<tr>
    <td>2</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>       
    <td>80</td>
</tr>
<tr>
    <td>3</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>
    <td>What is google? - Computer Hope 
www.computerhope.com/jargon/g/google.htm
Originally known as BackRub, Google is a search engine that started development in 1996 by Sergey Brin and <br/>
Larry Page as a research project at Stanford University.
What is Google (the company)? - Definition from WhatIs.com 
</td>       
    <td>67</td>
</tr>
</table>
</div>

CSS:

代码语言:javascript
复制
.table{ background:red; overflow-x:auto; }
table{ background:#aaffaa; border-collapse:collapse; display:block; }
th, td { border:1px solid #666; padding:10px; }

Javascript:

代码语言:javascript
复制
var i=0,row,table=document.getElementsByTagName('table')[0],tabwid=table.offsetWidth;

while(row=table.rows[i++])
{var hei=row.offsetHeight;
 while(tabwid<4000)
 {tabwid+=500;table.style.width=tabwid+'px';
  if(row.offsetHeight==hei)
  {table.style.width=tabwid-500+'px';break;}
 }
}

编辑:我发现可见表是和额外的空白(绿色空间)也是一部分。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-10 21:53:03

似乎可以处理JS中的以下更改

我添加了一个windowWidth变量,这样您就可以得到每个浏览器窗口的宽度。然后,不再像以前那样检查4000,而是检查浏览器窗口的大小。

代码语言:javascript
复制
  var i=0,row,table=document.getElementsByTagName('table')

    var windowWidth =  window.innerWidth;   

    [0],tabwid=table.offsetWidth;

     while(row=table.rows[i++])
     {var hei=row.offsetHeight;
         while(tabwid<windowWidth)
           {tabwid+=500;table.style.width=tabwid+'px';
             if(row.offsetHeight==hei)
           {table.style.width=tabwid-500+'px';break;}
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33059146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档