番茄的梦想

那一抹夕阳

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

可以预先设置好,再根据字符长度算出来

<script   language= "javascript ">
function   ByteWordCount(str)   {
str=   str.replace(/([\u0391-\uFFE5])/ig, '11 ');
return   str.length/2;
}

function   GetWidth(str){
return   ByteWordCount(str)*12+str.length*1;
}
</script>

<div   style= "font-size:12px;width:93px;border:1px   solid   #000;letter-spacing:   1px "   id= "div1 "> 中华人民共和国 </div>

<input   type= "button "   onclick= "alert(GetWidth(div1.innerText)); "   value= "看看宽度 ">

上边层里7个汉字,设置每个汉字12个像素,宽度是84,字间距1个像素,7个字符7个像素,84+7=91个像素,为什么上边层的宽度是93呢,因为还有两边边框的宽度,再加上2就正好是93了,层宽少一个像素就会换行

posted on 2011-08-01 10:23  番茄的梦想  阅读(2955)  评论(0编辑  收藏  举报