zoukankan      html  css  js  c++  java
  • textarea 高度自适应

    如何让textarea高度自适应,其实只要让textarea的高度随着滚动高度增加就好了

    获取高度

    $("textarea").height()

    获取滚动调试

    $("textarea").scrollHeight

    所以整体代码就是,注意textarea必须加overflow-y:hidden

    $("textarea").keyup(function(event) {
     $(this).css("height","auto");//加上这句话,这样在删除的时候,textarea的Height也可以动态变化
     if (this.scrollHeight > $(this).height()) {//this if is for ie
       $(this).height(this.scrollHeight);
     }					
    });
    

    这样写为什么可以实现这样的效果,在$(this).css("height","auto")后,跟踪一下height和scrollheight,发现height每次都会是它的初始高度,所以每次在判断的时候,保持了height的不变,而scrollheight却会随着内容的输入变化而变化,所以会实现这样的效果

  • 相关阅读:
    day10 测试2
    算法题
    day10 自动化测试
    day09 测试
    进行试验,对比
    多层网络的SIR实现
    day08 商城项目
    day07 cookie session 模板
    day06 模型类
    纸上谈兵:图(graph)
  • 原文地址:https://www.cnblogs.com/popping57/p/3318040.html
Copyright © 2011-2022 走看看