解決layui tips連續英文和數字不換行問題

JS


function view(value,id) {
  var width=getTextWidth(value);
  if(width>200){
      width='200px';
  }else {
      width='auto';
  }

  layer.tips('<span style="word-wrap: break-word;">'+value+'</span>',id, {
      tips: [1, '#0bcc0a'],
      time: 2000,
      area: [width, 'auto']
  });

}

//獲取一段文本具體長度爲多少px
function getTextWidth(str) {
    var width = 0;
    var html = document.createElement('span');
    html.innerText = str;
    html.className = 'getTextWidth';
    document.querySelector('body').appendChild(html);
    width = document.querySelector('.getTextWidth').offsetWidth;
    document.querySelector('.getTextWidth').remove();
    return width;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章