[英]How to prevent text from overflowing the cell
I am getting data from database and using that data creating row dynamically and attaching it to the table . 我从数据库中获取数据,并使用该数据动态创建行并将其附加到表中。 My code is as follows 我的代码如下
function addSpecToTable(name,id,direction,template,deployed)
{
var tbody = document.getElementById('tblSpecList').getElementsByTagName('TBODY')[0];
var row = document.createElement('TR');
var td1 = document.createElement('TD');
var td2 = document.createElement('TD');
var td4 =document.createElement('TD');
var td3 = document.createElement('TD');
td1.style.width="40%";
td1.style.paddingLeft="3px";
td1.innerText=name;
td2.style.width="20%";
td2.style.paddingLeft="3px";
td4.style.width="25%"
td4.style.paddingLeft="3px"
td3.style.width="15%"
td3.style.paddingLeft="3px"
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td4);
row.appendChild(td3);
tbody.appendChild(row);
} }
This name vareis in length.It's max length goes up to 70 characters. 此名称长度为vareis。最大长度为70个字符。 Bu after a certain characters it overshoot it's length and make table messy and that depends on characters for eg if it is all W then it's after the 32 W it will spoil the table structure so i want that before the text overshoot the length it shows ... and truncate rest of the character not fitting. 在某个字符之后,Bu过长了它的长度,使表变得凌乱,这取决于字符,例如,如果全是W,那么在32 W之后,它将破坏表结构,所以我希望在文本过长之前显示它的长度。 ..并截断其余部分不合适的字符。
How about pure css resolution: 纯CSS解析度如何:
#tblSpecList td {
/* essential */
text-overflow: ellipsis;
width: 200px;
white-space: nowrap;
overflow: hidden;
/* for good looks */
padding: 10px;
}
This will truncate long string and add 2 points at the end... 这将截断长字符串并在末尾添加2点...
Use jquery to truncate string after certain length. 使用jquery在一定长度后截断字符串。
<script>
$(document).ready(function(){
if ($('.tdclass').text().length > 70)
{
$('.tdclass').text(
$('.tdclass').text().substring(0,70)+"..." );
}
});
</script>
hope it will help 希望对你有帮助
var div = document.crateElement('div');
div.className = 'name-wrapper'
div.innerText=name;
td1.appendChild(div);
.name-warpper {
display:block;
word-wrap:break-word;
overflow:hidden;
width:300px // give some value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.