In the page often encounter text is too long, ellipsis is displayed beyond the part, the mouse slide over the text prompt box, but I will forget every time, now record, for the next time forgotten to refer to. The first:

Const getTextLen = STR => {if (! str ) return let str_length = 0 let str_len = str_length for (let i = 0; i < str_len; I ++) {let a = str.chartat (I) str_length++ // If (escape(a).length > 4) {str_length++}} return Const getTextRealWidth = (text,style) => {const span = document.createElement('span') span.innerText = text Object.keys(style).forEach(key=>{ span.style[key] = style[key] if(key === 'fontSize' && 'number' === typeof style[key] { span.style[key] = String(style[key]0 + 'px' } }) document.body.appendChild(span) const width = span.offsetWidth document.body.removeChild(span) return width } cosnt charWidthAverage = GetTextRealWidth ('Wm0123456789',totalStyles) For example, 88 const text = '88' const textWidth = getTextLen(text) * charWidthAverageCopy the code

The second: not sorted out temporarily