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> 

猜你喜欢

转载自songlitao.iteye.com/blog/2379150