jquery实现textarea高度自适应

```html

 

  textarea高度自适应,不依赖任何第三方插件

 

 

只关心逻辑不关心样式的文本框高度自定义


 

 

 

 

```

```javascript

void function () {

  function DiyArea (el) {

    this.el = el || {}

    this.config = {

      el: document.querySelectorAll('textarea')

    }

    this.init()

  }

  DiyArea.prototype = {

    init: function () {

      this.bindEvent()

    },

    bindEvent: function () {

      if (!this.config.el) {

        return false

      }

      if (this.config.el[0].addEventListener) {

        for (var i = 0; i < this.config.el.length; i++) {

          if (this.config.el[i].getAttribute('data-allow') === 'no-allow') {

            continue

          }

          this.config.el[i].addEventListener('input', function (e) {

            console.log(this.style.height)

            this.style.height = 'auto'

            this.style.height = this.scrollHeight + 'px'

          })

        }

      }

    }

  }

  return new DiyArea('diy-area')

}()

```

你可能感兴趣的:(jquery实现textarea高度自适应)