Home > Web Front-end > JS Tutorial > body text

jquery plug-in implements automatic height of multi-line text box [textarea]_jquery

WBOY
Release: 2016-05-16 16:11:20
Original
1135 people have browsed it

Function implementation:

1/Automatically increase the height of a line when the textarea wraps
2/When textarea deletes a line, the height of the line is automatically reduced. Dependency: jquery.xxx.js I need to use similar functions at work, but I find it inconvenient to use plug-ins to import other files, so I wrote one

textarea jquery plug-in

Copy code The code is as follows:




           


Copy code The code is as follows:

jQuery.extend({
textareaAutosize_dc: function() {
          $("textarea").on("keyup", function(e) {
               var currentEnterCount = $(this).val().split("n").length;
          var lineHeight = Number($(this).css("line-height").replace("px", ""));
            var enterCount = $(this).attr("enterCount");
If (currentEnterCount < enterCount && enterCount != undefined) {
//Subtract the fixed row height from each row
                    $(this).height($(this).height() - lineHeight);
                } else if (currentEnterCount > enterCount) {
//Add a fixed row height to each row
                     $(this).height($(this).height() lineHeight);
                     $(this).attr("enterCount", currentEnterCount);
            }
//Record the current row height
               $(this).attr("enterCount", currentEnterCount);
        });
}
});
//Call automatic height
$.textareaAutosize_dc();

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template