搜索了半天,求出字符串的length的倒是一下就找到了,显示宽的没能找到.有没有类似delphi里的Canvs.TextWidth的函数?请各位指点,拜谢~~~
解决方案 »
- jquery获取radio组选项的值?
- 谁能帮我解析一下extjs2.0 中,我已经拿到json的值并赋值给store了,想把这个store放到formpanel中
- 点复选框 beanwrite读取出来的数据可以修改
- 整数如何在后面补零(例如5变成5.00;5.1变成5.10)
- 求助~~用DOM生成script
- 返回上一页以后为何不执行body onLoad函数?
- 请大家帮忙:做E_mail的校验
- 网页real播放器无法播放?
- 在线等待,关于动态获取form item属性的数值
- 如何进行连续提交页面??
- 这种ID直接给封掉就可以了
- Safari 连续执行两次window.location=url的问题
---------------------------------------------------------
<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> 是怎么在JavaScript里用的?
我加到JavaScript里后 就卡死了
明显<SPAN id=span style= "font-size:9pt "> </span>是html部分,不是脚本明白原理自己就可以写了,就是用span标签赋值文本计算宽度,参考如下代码:
function textSize(fontSize, text) {
var span = document.createElement("span");
var result = {};
result.width = span.offsetWidth;
result.height = span.offsetWidth;
span.style.visibility = "hidden";
document.body.appendChild(span);
if (typeof span.textContent != "undefined")
span.textContent = text;
else span.innerText = text;
result.width = span.offsetWidth - result.width;
result.height = span.offsetHeight - result.height;
span.parentNode.removeChild(span);
return result;
}var size = textSize("9px", "囧");
alert("宽:" + size.width + " 高:" + size.height);
js获取其width应该可以吧
//span.style.fontSize=fontSize; 沒寫上而已
var result = {};result.width = span.offsetWidth;
result.height = span.offsetWidth;........result.width = span.offsetWidth - result.width;
result.height = span.offsetHeight - result.height;为什么要这么做呢?为什么不直接将text赋值给span,然后直接用赋值后的W和H呢?
总通不过呢