登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Shanmin's Blog

本Blog中所有文章除特别说明外均为本人原创,转载请注明!

 
 
 

日志

 
 

关于在<td>中显示文本的自动截断  

2009-11-02 19:54:22|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一直看到很多的地方用到这些自动截断的地方,以前也测试过,但是一直没有十分完美的实现过。手头的项目正好再次需要实现这个效果,这就促使了和同事共同研究这个问题,最终终于完美实现。

以前的代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<table width="100%" border="1" class="xTable">
  <tr>
    <td width="100">col_1</td>
    <td>col_2</td>
    <td width="120">col_3</td>
    <td width="100">col_4</td>
  </tr>
  <tr>
    <td>abcdefg</td>
    <td>a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z</td>
    <td>abcdefg</td>
    <td>abcdefg</td>
  </tr>
</table>
</body>
</html>

显示效果:

关于在td中显示文本的自动截断 - 0123 - Shanmins Blog

 

增加自动截断的代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
table
{
 table-layout:fixed;
}
td
{
 text-overflow:ellipsis;
 white-space:nowrap;
 overflow:hidden;
}
</style>
</head>

<body>
<table width="100%" border="1" class="xTable">
  <tr>
    <td width="100">col_1</td>
    <td>col_2</td>
    <td width="120">col_3</td>
    <td width="100">col_4</td>
  </tr>
  <tr>
    <td>abcdefg</td>
    <td>a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z</td>
    <td>abcdefg</td>
    <td>abcdefg</td>
  </tr>
</table>
</body>
</html>

显示效果:

关于在td中显示文本的自动截断 - 0123 - Shanmins Blog

 

学习到的东西:关键就是table-layout:fixed这行,以前每次测试失败的原因就是没有增加这个属性。

  评论这张
 
阅读(1281)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018