当前位置:Gxlcms > JavaScript > jquery文字填写自动高度的实现方法

jquery文字填写自动高度的实现方法

时间:2021-07-01 10:21:17 帮助过:5人阅读

下面开始写一个jquery插件

  1. (function($){
  2. $.fn.autoTextarea = function(options) {
  3. var defaults={
  4. maxHeight:null,//文本框是否自动撑高,默认:null,不自动撑高;如果自动撑高必须输入数值,该值作为文本框自动撑高的最大高度
  5. minHeight:$(this).height()
  6. };
  7. var opts = $.extend({},defaults,options);
  8. return $(this).each(function() {
  9. $(this).bind("paste cut keydown keyup focus blur",function(){
  10. var height,style=this.style;
  11. this.style.height = opts.minHeight + 'px';
  12. if (this.scrollHeight > opts.minHeight) {
  13. if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
  14. height = opts.maxHeight;
  15. style.overflowY = 'scroll';
  16. } else {
  17. height = this.scrollHeight;
  18. style.overflowY = 'hidden';
  19. }
  20. style.height = height + 'px';
  21. }
  22. });
  23. });
  24. };
  25. })(jQuery);

调用代码示例:

  1. <textarea name="textarea" id="textarea" cols="60" rows="4" class="chackTextarea-area"></textarea>
  2. <script type="text/javascript">
  3. $(".chackTextarea-area").autoTextarea({
  4. maxHeight: 220,//文本框是否自动撑高,默认:null,不自动撑高;如果自动撑高必须输入数值,该值作为文本框自动撑高的最大高度
  5. });

以上这篇jquery文字填写自动高度的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

人气教程排行