JavaScript code:
function autoEllipseText(element, text, width)
{
element.innerHTML = '' + text + '';
inSpan = document.getElementById('ellipsisSpan');
if(inSpan.offsetWidth > width)
{
var i = 1;
inSpan.innerHTML = '';
while(inSpan.offsetWidth < (width) && i < text.length)
{
inSpan.innerHTML = text.substr(0,i) + '...';
i++;
}
returnText = inSpan.innerHTML;
element.innerHTML = '';
return returnText;
}
return text;
}
Usage:
function setCellText() { cellElement = document.getElementById('textCell'); textBoxElement = document.getElementById('cellText'); cellElement.innerHTML = autoEllipseText(cellElement, textBoxElement.value, 80); }
References:
沒有留言:
張貼留言