javascript HTML 表格:仅在需要时在 td 中水平滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9698873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:30:43  来源:igfitidea点击:

HTML tables: horizontal scroll in a td only when needed

javascriptcssscrollhtml-tablecell

提问by ADM

I have a table with the following:

我有一张表,内容如下:

<table  cellpadding="2" cellspacing="0" > 
<tr>
<td>Contact: </td>
<td width="100px"><div style="overflow-x:scroll; width:100px">[email protected]</div>
</td>
</tr>
</table>

This code shows an horizontal scroll in the email cell.

此代码显示电子邮件单元格中的水平滚动。

When the email is a short one like [email protected] the scroll shows but it is not enabled as it is not needed, and when the email is longer let's say

当电子邮件是像 [email protected] 这样的简短电子邮件时,滚动显示但由于不需要它而未启用,当电子邮件较长时,我们说

[email protected]

[email protected]

the scroll enables so you can see the entire email.

滚动启用,因此您可以查看整个电子邮件。

This is good, but what I need is the scroll not to show at all when the email is a short one.

这很好,但我需要的是当电子邮件很短时根本不显示滚动条。

How can I do that??

我怎样才能做到这一点??

I've tried:

我试过了:

overflow-x:auto;

And it does not show the scroll when a short email but when the email is a long one it just cut it no scroll at all. I think this happens because there are no spaces in the email.

当一封简短的电子邮件时它不会显示滚动,但当电子邮件是一封长邮件时,它只是将其剪切而不滚动。 我认为这是因为电子邮件中没有空格

回答by Starx

By defining overflow-x: scrollyou are indicating for a scroll bar to appear no matter what.

通过定义,overflow-x: scroll您表示无论如何都会出现滚动条。

You should use overflow-x:auto. Here is a working demo

你应该使用overflow-x:auto. 这是一个工作演示

回答by Dementic

works like a charm (IE9)

像魅力一样工作(IE9)

<table cellpadding="2" cellspacing="0" >  
    <tr> 
    <td>Contact: </td> 
    <td width="100px"><div style="overflow:auto; width:100px">[email protected]</div> 
    </td> 
    </tr> 
</table> 

http://jsfiddle.net/fUW4s/1/

http://jsfiddle.net/fUW4s/1/