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後,便實現了擴展,每次被點擊時它會先彈出目前編輯裏的內容。

相關文章
相關標籤/搜索