用outerWidth把表头和表格内容相等

一、写表格的时候也会检查遇到一些问题,比如

     1、tbody内容过多需要出现横向滚动条

     2、表头需要固定,但是表头和tbody内容需要一起滚动

二、表格CSS样式写法

  table tbody{

   display:block;

   height:190px;

   overflow-y:scroll;

}

  table thead,tbody tr{

   display:table;

  width:100%;  

}

  tbale thead{

  width:calc(100%-em)

}

tbody td{

  overflow:hidden;

  text-overflow:ellipsis;

  -o-text-overflow:ellipsis;

  white-space:nowrap;

  word-woap:break-word;

     width:20%;

}

表头和表格tbody内容相等js

var ths=$('table thead th');//找到表头
var thsLength=ths.length;//把表头的长度保存在变量thsLength中
var td = $('table tbody tr td');//找到tbody的td
for(var i=0;i<td.length;i++){//遍历出tbody中每个td
$(ths[i]).outerWidth($(td[i]).outerWidth(true));//表头的宽度等于tbody td的宽度
}

 

posted @ 2017-01-11 11:34  jiayingzi  阅读(161)  评论(0编辑  收藏  举报