jquery实现文本框textarea自适应高度
网络编程 2021-07-04 21:04www.168986.cn编程入门
这篇文章主要介绍了jquery实现文本框textarea自适应高度的相关资料,需要的朋友可以参考下
浏览器中默认的文本框是不能根据内容的增多变高,只能固定高度有滚动条,体验不是很好,找了很多方法兼容都不行,总算找到个兼容良好的方法:
<body> <textarea id="textarea3" style="overflow-y:hidden; height:20px;resize: none"> </textarea> <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <script type="text/javascript"> $(function() { //最小高度和最大高度默认 $("#textarea1").textareaAutoHeight(); //最大高度为100px $("#textarea2").textareaAutoHeight({maxHeight: 100}); //最小高度为50px,最大高度为200px $("#textarea3").textareaAutoHeight({minHeight: 50, maxHeight: 200}); }) $.fn.extend({ textareaAutoHeight: function(options) { this._options = { minHeight: 0, maxHeight: 1000 } this.init = function() { for (var p in options) { this._options[p] = options[p]; } if (this._options.minHeight == 0) { this._options.minHeight = parseFloat($(this).height()); } for (var p in this._options) { if ($(this).attr(p) == null) { $(this).attr(p, this._options[p]); } } $(this).keyup(this.resetHeight).change(this.resetHeight) .focus(this.resetHeight); } this.resetHeight = function() { var _minHeight = parseFloat($(this).attr("minHeight")); var _maxHeight = parseFloat($(this).attr("maxHeight")); if (!$.browser.msie) { $(this).height(0); } var h = parseFloat(this.scrollHeight); h = h < _minHeight ? _minHeight :h > _maxHeight ? _maxHeight : h; $(this).height(h).scrollTop(h); if (h >= _maxHeight) { $(this).css("overflow-y", "scroll"); } else { $(this).css("overflow-y", "hidden"); } } this.init(); } }); </script> </body>
以上就是本文的全部内容,希望对大家学习jquery程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程