搜索 |
开发中,有时候用富文本粘贴的表格或者其他内容,样式常常是我们不能控制的,比如粘贴一个表格,表格某个列的内容很长,自适应的话会让表格的内容少的列挤得变形,而实际他可能只是数字列,我们不希望他换行,既可以试试下面的方法。
$(function(){
$("table td").each(function(){
//移除不需要的属性,可以根据实际需要增减
$(this).removeAttr("style") //移除style属性,用CSS去设置
.removeAttr('class') //移除class样式以后可以自己设置公用样式
.removeAttr('face') //移除face标签,用CSS去设置
.removeAttr('width') // 移除width属性,用CSS去设置
.removeAttr("x:str") //移除粘贴后office属性
.removeAttr("x:num") //移除粘贴后office属性
.removeAttr("height") //移除height属性,用CSS去设置
.removeAttr("size"); //移除size标签,用CSS去设置
if(
//不是中文且长度不超过50
(!/[\u4e00-\u9fa5]/g.test($.trim($(this).text()))
&& $.trim($(this).text()).length<50)
||
//小于特定长度的字符
($.trim($(this).text()).length<14)
){
//用一个div包裹,防止其他意外
$(this).wrapInner("<div style='white-space:nowrap'></div>")
}
if(
//全是数字检测
/^\d+(\.\d+)?$/g.test($.trim($(this).text()))
&& $.trim($(this).text()).length>2
){
$(this).css('textAlign','right')
}
})
//占位元素
$(":empty").attr('style','font-size:0px !important')
})