您的代码有几个问题。
- 为什么
/ 5?字符的宽度取决于font-family和font-size。 
- 您必须在缩写标题中转义
str(否则 " 将使代码无效)。 
diff未声明并在全局范围内结束 
substring不应该那样工作。你使用的是什么浏览器? 
hidden不是 的有效值style.display。要隐藏它,您应该使用该值none,但浏览器不计算offsetWidth. 改为使用style.visibility="hidden"。 
- 寻找合适的长度是非常低效的。
 
- 必须逃走
</abbr>” 
我为您重写并添加了它,className以便您可以使用样式来设置font-familyand font-size。Fooz 先生建议您使用鼠标悬停来显示整个字符串。这不是必需的,因为现代浏览器会为您执行此操作(已使用 FF、IE、Opera 和 Chrome 进行测试)
    function fitStringToSize(str,len,className) {
    var result = str; // set the result to the whole string as default
    var span = document.createElement("span");
    span.className=className; //Allow a classname to be set to get the right font-size.
    span.style.visibility = 'hidden';
    span.style.padding = '0px';
    document.body.appendChild(span);
    // check if the string don't fit 
    span.innerHTML = result;
    if (span.offsetWidth > len) {
        var posStart = 0, posMid, posEnd = str.length;
        while (true) {
            // Calculate the middle position
            posMid = posStart + Math.ceil((posEnd - posStart) / 2);
            // Break the loop if this is the last round
            if (posMid==posEnd || posMid==posStart) break;
            span.innerHTML = str.substring(0,posMid) + '…';
            // Test if the width at the middle position is
            // too wide (set new end) or too narrow (set new start).
            if ( span.offsetWidth > len ) posEnd = posMid; else posStart=posMid;
        }
        //Escape
        var title = str.replace("\"",""");
        //Escape < and >
        var body = str.substring(0,posStart).replace("<","<").replace(">",">");
        result = '<abbr title="' + title + '">' + body + '…<\/abbr>';
    }
    document.body.removeChild(span);
    return result;
    }
编辑:在进行更多测试时,我发现了一些错误。
改进:
- 转义复制到所有位置跨度的字符串。您仍然可以使用 html-entities,但不允许使用标签(
<并将>显示) 
- 重写了
while-statement(它有点快,但主要是为了摆脱导致额外回合的错误并摆脱break-statement) 
- 将函数重命名为
fitStringToWidth 
版本 2:
function fitStringToWidth(str,width,className) {
  // str    A string where html-entities are allowed but no tags.
  // width  The maximum allowed width in pixels
  // className  A CSS class name with the desired font-name and font-size. (optional)
  // ----
  // _escTag is a helper to escape 'less than' and 'greater than'
  function _escTag(s){ return s.replace("<","<").replace(">",">");}
  //Create a span element that will be used to get the width
  var span = document.createElement("span");
  //Allow a classname to be set to get the right font-size.
  if (className) span.className=className;
  span.style.display='inline';
  span.style.visibility = 'hidden';
  span.style.padding = '0px';
  document.body.appendChild(span);
  var result = _escTag(str); // default to the whole string
  span.innerHTML = result;
  // Check if the string will fit in the allowed width. NOTE: if the width
  // can't be determined (offsetWidth==0) the whole string will be returned.
  if (span.offsetWidth > width) {
    var posStart = 0, posMid, posEnd = str.length, posLength;
    // Calculate (posEnd - posStart) integer division by 2 and
    // assign it to posLength. Repeat until posLength is zero.
    while (posLength = (posEnd - posStart) >> 1) {
      posMid = posStart + posLength;
      //Get the string from the beginning up to posMid;
      span.innerHTML = _escTag(str.substring(0,posMid)) + '…';
      // Check if the current width is too wide (set new end)
      // or too narrow (set new start)
      if ( span.offsetWidth > width ) posEnd = posMid; else posStart=posMid;
    }
    result = '<abbr title="' +
      str.replace("\"",""") + '">' +
      _escTag(str.substring(0,posStart)) +
      '…<\/abbr>';
  }
  document.body.removeChild(span);
  return result;
}