JS计算任意字符串宽度
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.8.3.min.js"></script>
</head>
<body>
<div id='labelText' style='color:black;line-height:1.2;white-space:nowrap;top:0px;left:0px;position:fixed;display:block;visibility:visible;'>
</div>
<script>
var str="12,,009sss啊啊";
str = str.substring(0,str.length);
$("#labelText").css({
"font-size": "12px",
"font-family": "Microsoft YaHei"
}).html(str);
var width = $("#labelText").width();
alert(width);
</script>
</body>
</html>
转:https://blog.csdn.net/song_litao/article/details/84901565?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-2-84901565.pc_agg_new_rank&utm_term=css%E8%AE%A1%E7%AE%97%E5%AD%97%E7%AC%A6%E7%9A%84%E5%AE%BD%E5%BA%A6&spm=1000.2123.3001.4430
浙公网安备 33010602011771号