js控制textarea文本域自适应高度

本文介绍了一个自适应文本区域的实现方案,通过JavaScript监听文本框的各种事件,并调整其高度以适应内容的变化,确保输入内容完全可见。
<!DOCTYPE html>
<html>
<head>
<title>autoresizing textarea</title>
<style type="text/css">
textarea {
width:360px;
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
resize: none;
margin-top:30px;
margin-left:400px;
border-radius:5px;
}
</style>
<script type="text/javascript">
var observe;
if (window.attachEvent) {
    observe = function (element, event, handler) {
        element.attachEvent('on'+event, handler);
    };
}
else {
    observe = function (element, event, handler) {
        element.addEventListener(event, handler, false);
    };
}
function init () {
    var text = document.getElementById('text');
    function resize () {


var he = text.style.height;
//获取文本框本本身高度
var h = he.substring(0,he.indexOf('p'));


if(text.scrollHeight > h){
text.style.height = text.scrollHeight+'px';
}else{
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
    }
    /* 0-timeout to get the already changed text */
    function delayedResize () {
        window.setTimeout(resize, 0);
    }
    observe(text, 'change',  resize);
    observe(text, 'cut',     delayedResize);
    observe(text, 'paste',   delayedResize);
    observe(text, 'drop',    delayedResize);
    observe(text, 'keydown', delayedResize);


    text.focus();
    text.select();
    resize();
}
</script>
</head>
<body onload="init();">
<textarea  id="text" rows="5"></textarea>
</body>
</html>
在IE浏览器中实现文本域自适应数据高度可以采用以下几种方法: #### 方法一:使用JavaScript动态调整高度 通过监听文本域的输入事件,动态计算文本内容的高度并调整文本域高度。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>IE Textarea Auto Height</title> <style> textarea { resize: none; overflow: hidden; } </style> </head> <body> <textarea id="myTextarea"></textarea> <script> var textarea = document.getElementById('myTextarea'); textarea.oninput = function () { this.style.height = 'auto'; this.style.height = this.scrollHeight + 'px'; }; // 初始化高度 textarea.oninput(); </script> </body> </html> ``` 在上述代码中,首先将文本域的 `resize` 属性设置为 `none` 以禁止用户手动调整大小,`overflow` 属性设置为 `hidden` 避免出现滚动条。然后监听 `input` 事件,在事件处理函数中先将文本域高度设置为 `auto`,再将其高度设置为 `scrollHeight`,这样就能保证文本域高度自适应内容高度。最后手动触发一次 `input` 事件来初始化高度。 #### 方法二:使用jQuery实现 如果项目中使用了jQuery,可以使用以下代码实现: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>IE Textarea Auto Height with jQuery</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> textarea { resize: none; overflow: hidden; } </style> </head> <body> <textarea id="myTextarea"></textarea> <script> $(document).ready(function () { $('#myTextarea').on('input', function () { $(this).css('height', 'auto'); $(this).css('height', this.scrollHeight + 'px'); }).trigger('input'); }); </script> </body> </html> ``` 这里使用了jQuery的 `on` 方法监听 `input` 事件,在事件处理函数中同样先将文本域高度设置为 `auto`,再设置为 `scrollHeight`。最后使用 `trigger` 方法触发 `input` 事件来初始化高度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值