js用document.getElementById获取的值显示为[object HTMLInputElement] javascriptobjectjsfunction字体 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 getElementById得到的是对象,要获取value才行。。为空就是你控件没有值咯。。而且你的动态语句做了判断了吧,如果<#if HOME_PAGE.orgName??>不成立就连那个对象都没有了 而且你的对象的style没有定义过fontSize,如果是css定义了font-size,要通过currentStyle或者getcomputedStyle获取,直接style.fontSize获取不到style.width也一样,还有input对象没innerHTML,得到的为空。。 function titleZoom() { var obj = document.getElementById("orgName"); //获得div内的字体长度 var len = obj.value //获得div设置的字体大小 var currentSize = obj.style.fontSize; if (!currentSize) { if (obj.currentStyle) currentSize = obj.currentStyle['fontSize']; else currentSize = window.getComputedStyle(obj, null).fontSize; } alert(obj); alert(currentSize); alert(len); //去掉px currentSize = currentSize.substr(0, currentSize.length - 2); //计算字体所占的宽度 var frontWidth = currentSize * (len); //去掉px var divWidth = obj.style.width.substr(0, obj.style.width.length - 2); //如果默认设置的字体大小大于div的宽度,则重新计算div内字体的大小 if (frontWidth > divWidth) { var fontSize = divWidth / (len + 2); obj.style.fontSize = fontSize + 'px'; } } fontsize解决了,谢谢您,还有一个问题,获得input内的字体长度len,刚才运行下什么都没有,没有得到,这个该怎么获取啊 input没innerHTML,显示的内容是value属性,不是一起修改了。。 刚才拉个词 var len = obj.value.length这样对了 打出len是零 但是我明明有值啊 你的看发到客户端html代码是否有值了自己检查下,而且input控件少了type属性了吧。。 <input class="input_view" value="${HOME_PAGE.orgName}" readOnly="true" style="color:${HOME_PAGE.color}" id="orgName" type="hidden"/> obj.value是什么都没有得到,问题是确实有值啊 那你应该是有多个id相同的对象了,获取到的是第一个对象检查下你那个元素之前是否有id一样的控件 js ztree 求助 百度Map maker 问题 麻烦有做过的大神指导下,谢谢 jsp页面表格求和用js怎么实现? 帮忙弄个正则验证。。 Ext 中if为何不起作用 removeEventListener如何注销所有事件? javascript 代码在html中,执行的理解? window.open打开的字窗口中怎么调用父窗口中的javascript 函数 window.open()??? mozilla下如何判断用户按键的码值 用JS验证登陆,表单是否为空,链接数据库那种 js 代码中ready function 执行不成功
var obj = document.getElementById("orgName");
//获得div内的字体长度
var len = obj.value
//获得div设置的字体大小
var currentSize = obj.style.fontSize;
if (!currentSize) {
if (obj.currentStyle) currentSize = obj.currentStyle['fontSize'];
else currentSize = window.getComputedStyle(obj, null).fontSize;
}
alert(obj);
alert(currentSize);
alert(len);
//去掉px
currentSize = currentSize.substr(0, currentSize.length - 2);
//计算字体所占的宽度
var frontWidth = currentSize * (len);
//去掉px
var divWidth = obj.style.width.substr(0, obj.style.width.length - 2);
//如果默认设置的字体大小大于div的宽度,则重新计算div内字体的大小
if (frontWidth > divWidth) {
var fontSize = divWidth / (len + 2);
obj.style.fontSize = fontSize + 'px';
}
}