博客
关于我
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/

你可能感兴趣的文章
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现huffman哈夫曼编码算法(附完整源码)
查看>>
Objective-C实现ID3贪心算法(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>
Objective-C实现integer partition整数分区算法(附完整源码)
查看>>
Objective-C实现integerPartition整数划分算法(附完整源码)
查看>>
Objective-C实现interpolation search插值搜索算法(附完整源码)
查看>>
Objective-C实现Interpolation search插值查找算法(附完整源码)
查看>>
Objective-C实现intersection交集算法(附完整源码)
查看>>
Objective-C实现intro sort内省排序算法(附完整源码)
查看>>
Objective-C实现inverse matrix逆矩阵算法(附完整源码)
查看>>
Objective-C实现inversions倒置算法(附完整源码)
查看>>
Objective-C实现isalpha函数功能(附完整源码)
查看>>
Objective-C实现islower函数功能(附完整源码)
查看>>
Objective-C实现isPowerOfTwo算法(附完整源码)
查看>>
Objective-C实现isupper函数功能(附完整源码)
查看>>
Objective-C实现ItemCF算法(附完整源码)
查看>>
Objective-C实现ItemCF算法(附完整源码)
查看>>