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

你可能感兴趣的文章
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>