/** * 根據字符串寬度截取字符串 * @param desc 原始字符串 * @param width 該顯示的寬度 * @param fontsize 字體大小 12px * @returns {string} */ function cutStringByWith(desc,width,fontsize) { if($('#word-cut').length==0){ $('body').append('<span id="word-cal" style="visibility: hidden; white-space: nowrap;font-size: '+fontsize+';"></span> ') } var temp_desc="";//存放截斷字符串 for(var j=0;j<desc.length;j++){ //desc是目標字符串, temp_desc+=desc[j]; $('#word-cut').text(temp_desc); if($('#word-cut').width()>width){ break; } } temp_desc+=".."; return temp_desc; }