博客
关于我
jquery动态改变textarea高度,高度随着内容增加而自动变高,内容减少而自动变低
阅读量:294 次
发布时间:2019-03-03

本文共 635 字,大约阅读时间需要 2 分钟。

jQuery代码实现自适应高度 textarea

作为开发人员,以下 jQuery 实现可以帮助 textarea 自动调整高度,提升用户体验。以下代码可直接复制使用,需在使用前先注册方法。

jQuery.fn.extend({

autoHeight: function() {
return this.each(function() {
var $this = jQuery(this);
if (!$this.attr('_initAdjustHeight')) {
$this.attr('_initAdjustHeight', $this.outerHeight());
}
_adjustH($this).on('input', function() {
_adjustH($this);
});
});
}
});

function _adjustH(elem) {

var $obj = jQuery(elem);
return $obj.css({
height: $obj.attr('_initAdjustHeight'),
'overflow-y': 'hidden'
}).height(elem.scrollHeight);
}

在项目中使用时,可通过以下方式调用:

$(function() {

$('textarea').autoHeight();
});

该实现通过设置初始高度并响应输入事件,确保 textarea 始终保持合适的尺寸。

转载地址:http://bxyl.baihongyu.com/

你可能感兴趣的文章
oracle 批量生成建同义词语句和付权语句
查看>>
oracle 抓包工具,shell 安装oracle和pfring(抓包) 及自动环境配置
查看>>
Oracle 拆分以逗号分隔的字符串为多行数据
查看>>
Oracle 排序中使用nulls first 或者nulls last 语法
查看>>
oracle 插入date日期类型的数据、插入从表中查出的数据,使用表中的默认数据
查看>>
Oracle 操作笔记
查看>>
oracle 数据库 安装 和优化
查看>>
oracle 数据库dg搭建规范1
查看>>
Oracle 数据库常用SQL语句(1)
查看>>
Oracle 数据库特殊查询总结
查看>>
Oracle 数据类型
查看>>
Oracle 数据自动备份 通过EXP备份
查看>>
oracle 数据迁移 怎么保证 和原表的数据顺序一致_一个比传统数据库快 1001000 倍的数据库,来看一看?...
查看>>
oracle 时间函数
查看>>
oracle 时间转化函数及常见函数 .
查看>>
Oracle 权限(grant、revoke)
查看>>
oracle 查询clob
查看>>
Oracle 比较 B-tree 和 Bitmap 索引
查看>>
Oracle 注意点大全
查看>>
UML- 组件图(构件图)
查看>>