用JS scrollHeight方法实现textarea输入高度自适应的方法
来源:互联网 发布:windows 10 iso bt 编辑:程序博客网 时间:2024/04/29 02:31
HTML源代码部分:
<textarea id="content" name="content" onkeyup="autosize(this); checkSupply(4000,'content');" default_height="94">{$answer['content']}</textarea>
/*html中focus函数也经过测试不好使。*/
//textarea高度自适应
function autosize(obj) {var d_h = parseInt(obj.attributes['default_height'].value);
if(obj.scrollHeight > d_h) {
alert(obj.scrollHeight);
obj.style.height = obj.scrollHeight + 'px';
} else {
obj.style.height = d_h + 'px';
}
}
这个方法适应FF。
但有一个BUG。在IE浏览器下面,scrollHeight值有偏差它的scrollHeight
会莫名奇妙地多出一些,看起来非常奇怪。。
一直在网上寻找解决方案无果。
最后经一位大神提醒,修改了一下解决方案。。
首先模拟一个DIV。获取textarea内容,实时赋值给这个DIV。然后取它的高度再将其高度给textarea
html部分:
<textarea default_height="94" onkeyup="autosize('description'); checkSupply(3000,'description');" name="description" id="description" style="height: 114px; padding-top: 20px;"></textarea>
首先模拟一个DIV。获取textarea内容,实时赋值给这个DIV。取它的高度再将其高度给textareafunction autosize(obj) { var d_h = parseInt($('#'+obj).attr('default_height')); var mnDiv ='<div class="mnDiv"></div>'; $('.mnDiv').remove(); $('#'+obj).after(mnDiv); $('.mnDiv').css({width:$('#'+obj).width(),display:"none"}).html('<pre>'+$('#'+obj).val()+'</pre>');//填充内容 var mnDivHeight =$('.mnDiv').height(); if(mnDivHeight > d_h) {// alert($('#'+obj)[0].scrollHeight); $('#'+obj).css({'height':mnDivHeight,'padding-top':'20px'}); } else { $('#'+obj).css({'height':d_h}); }}
0 0
- 用JS scrollHeight方法实现textarea输入高度自适应的方法
- 学习到textarea高度自适应的方法
- js实现textarea自适应高度
- 用DIV+CSS模拟textarea,实现文本框高度自适应用户输入的文本
- js实现文本框textarea高度自适应
- js实现textarea高度根据内容自适应
- js实现textarea高度根据内容自适应
- jquery/js实现文本框根据输入内容input,textarea自适应高度
- textarea输入框自适应高度
- textarea的高度自适应
- textarea高度自适应增高;chrome下clientHeight,offsetHeight和scrollHeight
- js版本textarea自适应高度
- js实现textarea根据内容大小自适应高度
- textarea如何实现高度自适应?
- jquery实现textarea高度自适应
- textarea如何实现高度自适应?
- textarea如何实现高度自适应
- textarea如何实现高度自适应
- 关于 No operations allowed after connection closed.错误
- [Elasticsearch] 多字段搜索 (四) - 跨字段实体搜索
- 关于FD_CLOEXEC的一个故事 http://leave001.blog.163.com/blog/static/162691293201251810343121/
- CGRectInset的使用
- Vyatta2.0发布 博科押宝SDN
- 用JS scrollHeight方法实现textarea输入高度自适应的方法
- 各种数组排序方法
- 解决远程连接mysql错误1130
- [Elasticsearch] 多字段搜索 (五) - 以字段为中心的查询
- Look and Say-3438
- 写给大家看的项目管理书(第3版)——互动出版网
- jsp中的四大作用域
- leetcode Pascal's Triangle
- js创建Form表单并提交