jquery 插件實(shí)現(xiàn)多行文本框[textarea]自動(dòng)高度

字號(hào):


    這篇文章主要介紹了jquery 插件實(shí)現(xiàn)多行文本框[textarea]自動(dòng)高度,需要的朋友可以參考下
    實(shí)現(xiàn)功能:
    1/當(dāng)textarea換行時(shí)自動(dòng)增加一行高度
    2/當(dāng)textarea刪除一行時(shí) 自動(dòng)減少一行的高度 依賴:jquery.xxx.js 工作中需要使用類似功能但是覺(jué)得使用插件需要導(dǎo)入其他文件很不方便所以就寫(xiě)了一個(gè)
    textarea jquery插件
    代碼如下:
    <div>
    <label for="form-field-5"> 內(nèi)容</label>
    <div>
    <textarea id="form-field-5" placeholder="請(qǐng)輸入內(nèi)容..."></textarea>
    </div>
    </div>
    代碼如下:
    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) {
    //每行減掉固定行高
    $(this).height($(this).height() - lineHeight);
    } else if (currentEnterCount > enterCount) {
    //每行加入固定行高
    $(this).height($(this).height() + lineHeight);
    $(this).attr("enterCount", currentEnterCount);
    }
    //記錄當(dāng)前行高
    $(this).attr("enterCount", currentEnterCount);
    });
    }
    });
    //調(diào)用自動(dòng)高度
    $.textareaAutosize_dc();
    以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。