当textarea在此代码中展开时,如何执行函数?

时间:2015-04-14 04:06:35

标签: javascript jquery html

我有以下代码用于扩展textarea,我没有问题扩展textarea基于文本长度,但是,我需要在扩展时执行一个函数。我不知道应该使用哪个术语来做到这一点。像if(这个textarea扩展){alert('ok');欣赏。

$.fn.TextAreaExpander = function(minHeight, maxHeight) {

			var hCheck = !($.browser.msie || $.browser.opera);

			// resize a textarea
			function ResizeTextarea(e) {

				// event or initialize element?
				e = e.target || e;

				// find content length and box width
				var vlen = e.value.length, ewidth = e.offsetWidth;
				if (vlen != e.valLength || ewidth != e.boxWidth) {

					if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0";
					var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax));

					e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
					e.style.height = h + "px";

					e.valLength = vlen;
					e.boxWidth = ewidth;
					
				}

				return true;
			};

			// initialize
			this.each(function() {

				// is a textarea?
				if (this.nodeName.toLowerCase() != "textarea") return;

				// set height restrictions
				var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
				this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
				this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);

				// initial resize
				ResizeTextarea(this);

				// zero vertical padding and add events
				if (!this.Initialized) {
					this.Initialized = true;
					$(this).css("padding-top", 0).css("padding-bottom", 0);
					$(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
				}
				
			});

			return this;
		};
		jQuery("textarea[class*=expand9-999]").TextAreaExpander();//initialize the text expand
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea class="expand9-999">good</textarea>

2 个答案:

答案 0 :(得分:1)

尝试以下内容可以帮助您。

$(document).ready(function () {
var $textareas = jQuery('textarea');

    // set init (default) state   
    $textareas.data('x', $textareas.outerWidth());
    $textareas.data('y', $textareas.outerHeight());

    $textareas.mouseup(function () {

        var $this = $(this);

        if ($this.outerWidth() != $this.data('x') || $this.outerHeight() != $this.data('y')) {
            alert("ok");
        }

        // set new height/width
        
    });
  });

textareaResize($(".expand9-999"));

$.fn.TextAreaExpander = function(minHeight, maxHeight) {

			var hCheck = !($.browser.msie || $.browser.opera);

			// resize a textarea
			function ResizeTextarea(e) {

				// event or initialize element?
				e = e.target || e;

				// find content length and box width
				var vlen = e.value.length, ewidth = e.offsetWidth;
				if (vlen != e.valLength || ewidth != e.boxWidth) {

					if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0";
					var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax));

					e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
					e.style.height = h + "px";

					e.valLength = vlen;
					e.boxWidth = ewidth;
					
				}

				return true;
			};

			// initialize
			this.each(function() {

				// is a textarea?
				if (this.nodeName.toLowerCase() != "textarea") return;

				// set height restrictions
				var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
				this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
				this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);

				// initial resize
				ResizeTextarea(this);

				// zero vertical padding and add events
				if (!this.Initialized) {
					this.Initialized = true;
					$(this).css("padding-top", 0).css("padding-bottom", 0);
					$(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
				}
				
			});

			return this;
		};
		jQuery("textarea[class*=expand9-999]").TextAreaExpander();//initialize the text expand
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea class="expand9-999">good</textarea>

答案 1 :(得分:1)

您需要保存height并使用之前保存的身高检查当前height

仅供参考 - 您需要包含jQuery Migrate $.browser.msie库才能正常工作。 Reference

工作代码段:

$.fn.TextAreaExpander = function(minHeight, maxHeight) {

  var hCheck = !($.browser.msie || $.browser.opera);

  var prevHeight;
  
  // resize a textarea
  function ResizeTextarea(e) {

    // event or initialize element?
    e = e.target || e;

    // find content length and box width
    var vlen = e.value.length, ewidth = e.offsetWidth;
    if (vlen != e.valLength || ewidth != e.boxWidth) {

      if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0";
      
      var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax));

      e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
      e.style.height = h + "px";  // this is where you are actually resizing
      
      if(e.style.height !== prevHeight)  // throw the alert only if the height is not same as the previous one
        alert("resized");

      e.valLength = vlen;
      e.boxWidth = ewidth;
      
      prevHeight = e.style.height;  // save the height

    }

    return true;
  };

  // initialize
  this.each(function() {

    // is a textarea?
    if (this.nodeName.toLowerCase() != "textarea") return;

    // set height restrictions
    var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
    this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
    this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);

    // initial resize
    ResizeTextarea(this);

    // zero vertical padding and add events
    if (!this.Initialized) {
      this.Initialized = true;
      $(this).css("padding-top", 0).css("padding-bottom", 0);
      $(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
    }

  });

  return this;
};
jQuery("textarea[class*=expand9-999]").TextAreaExpander();//initialize the text expand
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

<textarea class="expand9-999">good</textarea>

<p>Check your console.</p>