<!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>
js实现textarea自适应高度
最新推荐文章于 2024-06-25 10:18:33 发布