textarea使用心得

做者一直觉得textarea只能够做为多行文本的输入框,今天才知道原来textarea能够显示提示的文本内容。javascript

在显示内容的时候,能够实现css

(1)隐藏边框java

(2)隐藏右下角的拉伸边框jquery

(3)自适应高度函数

下面是代码,须要css、js配合this

<style type="text/css"> textarea { width: 580px; resize:none; border:0px solid; line-height: 1.5em; color:#333; font-size: 14px; margin-top: 10px; padding: 0px; overflow-y:hidden;
    }
</style>
<script type="text/javascript"> $(document).ready(function() { $('.statuses').each(function() { this.style.height = 'auto'; this.style.height = this.scrollHeight+'px'; }); }); </script>
<textarea class="statuses" readonly >内容</textarea>

<textarea>输入框,也但愿能自适应高度,就须要js配合,做者收集了一种方法spa

<script> $.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>
<textarea id="textarea1"></textarea>
<textarea id="textarea2"></textarea>
<textarea id="textarea3"></textarea>
<script>
    //最小高度和最大高度默认
 $("#textarea1").textareaAutoHeight(); //最大高度为100px
 $("#textarea2").textareaAutoHeight({ maxHeight:100 }); //最小高度为50px,最大高度为200px
 $("#textarea3").textareaAutoHeight({ minHeight:50, maxHeight:200 }); </script>

若是对jquery代码中的$.fn.extend含义不理解,网上的一片文章简要介绍了jquery扩展,做者在这里引用一下:prototype

jQuery为开发插件提拱了两个方法,分别是:插件

jQuery.fn.extend();code

jQuery.extend();

虽然 javascript 没有明确的类的概念,可是用类来理解它,会更方便。

jQuery即是一个封装得很是好的类,好比咱们用 语句 $("#btn1") 会生成一个 jQuery类的实例。

jQuery.extend(object); 为jQuery类添加类方法,能够理解为添加静态方法。如:

jQuery.extend({

min: function(a, b) { return a < b ? a : b; },

max: function(a, b) { return a > b ? a : b; }

});

ObjectjQuery.extend( target, object1, [objectN])用一个或多个其余对象来扩展一个对象,返回被扩展的对象

结果:jQuery.min(2,3); //  2 
jQuery.max(4,5); //  5

var settings = { validate: false, limit: 5, name: "foo" }; 
var options = { validate: true, name: "bar" }; 
jQuery.extend(settings, options);settings == { validate: true, limit: 5, name: "bar" }

 

jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例能够使用这个“成员函数”。

好比咱们要开发一个插件,作一个特殊的编辑框,当它被点击时,便alert 当前编辑框里的内容。能够这么作:

$.fn.extend({          
     alertWhileClick:function() {            
           $(this).click(function(){                 
                  alert($(this).val());           
            });           
      }       
});       
$("#input1").alertWhileClick(); // 页面上为:    

$("#input1") 为一个jQuery实例,当它调用成员方法 alertWhileClick后,便实现了扩展,每次被点击时它会先弹出目前编辑里的内容。

相关文章
相关标签/搜索