如果要取得精确的宽度可能不容易,因为受字体等的影响但有一个笨一点的方法,用一个HTML对象,如SPAN,先记录下SPAN的offsetWidth,位置把字符放到innerText,再取offsetWidth,这两个offsetWidth的差值就是字符的宽度如:
<SPAN id=span style="font-size:9pt"></span>var o=document.getElementById("span");
var oldWidth=o.offsetWidth;
o.innerText="字符";
var width=o.offsetWidth-oldWidth;alert(“宽:”+width)
以上未经过测试
<SPAN id=span style="font-size:9pt"></span>var o=document.getElementById("span");
var oldWidth=o.offsetWidth;
o.innerText="字符";
var width=o.offsetWidth-oldWidth;alert(“宽:”+width)
以上未经过测试
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货