您的位置:首页 > Web前端 > JavaScript

js实现textarea自适应高度

2017-07-31 16:18 387 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
#t{
outline:none;
border:thin solid rgba(82, 168, 236, 0.8);
box-shadow: 0 0 6px rgba(82, 168, 236, 0.6);
resize: none;
width: 500px;
height: 20px;
font-size: 16px;
}
</style>
</head>
<body>
<textarea name="textArea" id="t"></textarea>
<script type="text/javascript">
var textArea = document.getElementById('t');
function activeGo() {
var scrollHeight = textArea.scrollHeight;
var height = textArea.offsetHeight;
if(height < scrollHeight){
textArea.style.height = textArea.scrollHeight+'px';
}
}
textArea.addEventListener('propertychange',activeGo);
textArea.addEventListener('input',activeGo);
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript