js根据输入字符长度自动调整textarea高度
2018-03-07 11:24
441 查看
1.编写html
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>自动扩展文本框高度</title>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("#words").on("input propertychange",function(e){
var target = e.target;
var dh = $(target).attr('defaultHeight') || 0;
if (!dh) {
dh = target.clientHeight;
$(target).attr('defaultHeight', dh);
}
target.style.height = dh + 'px';
var clientHeight = target.clientHeight;
var scrollHeight = target.scrollHeight;
if (clientHeight !== scrollHeight) {
target.style.height = scrollHeight + 10 + "px";
}
});
});
</script>
</head>
<body style="text-align: center">
<label><h3>请输入:</h3></label><br>
<textarea id="words" name="words" style="width: 300px;height: 30px;" default-height="20px"></textarea>
</body>
</html>
2.调试
2.1绑定函数前效果
2.2绑定函数后效果
相关文章推荐
- JS限制textbox或textarea输入字符长度
- Autosize插件允许textarea元素根据录入的内容自动调整元素的高度
- jquery mobaile中textarea根据内容自动调整高度
- Jquery实现 TextArea 文本框根据输入内容自动适应高度
- js控制textarea输入框根据内容自动适应高度
- cell根据文本长度来自动调整cell高度
- UITableView详解(UITableViewCell(三) cell根据文本长度来自动调整cell高度)
- Jquery实现 TextArea 文本框根据输入内容自动适应高度
- textarea自动换行,且文本框根据输入内容自适应高度
- cell根据文本长度来自动调整cell高度
- JS判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- jquery/js实现文本框根据输入内容input,textarea自适应高度
- 初识ReactJS的组件化开发(八):用表单做评论 :限制textarea可输入字符长度
- 【JS】JS判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- 限制textbox或textarea输入字符长度的JS代码
- 用JS scrollHeight方法实现textarea输入高度自适应的方法
- js根据输入内容自动填充下拉框
- 设置textView根据输入的文字自动增加高度
- UILabel 根据文字长度自动调整
- JS验证控制输入中英文字节长度(input、textarea等)具体实例