美文网首页
vue中文本域限制字数的方法

vue中文本域限制字数的方法

作者: 哒哒哒哒da | 来源:发表于2019-11-04 15:06 被阅读0次
用watch来监听,实现操作也比较简单

HTML

<textarea  v-model="title" ></textarea>
<span>{{ title.length }}/200</span>

JS

<script>
export default {
  name: 'XXX',
    data() {
        return {
            title: '',
            titleMaxLength:200
        };
    },
    methods:{
    },
    watch: {
        // 文本域字数限制
        title() {
            if (this.title.length > this.titleMaxLength) {
                this.title = String(this.title).slice(0, this.titleMaxLength);
            }
        }
    }
}
</script>

CSS

textarea{
     width: 100%;
     height: 60px;
     border: none;
     outline: none;
     box-sizing: border-box;
}

相关文章

网友评论

      本文标题:vue中文本域限制字数的方法

      本文链接:https://www.haomeiwen.com/subject/culdbctx.html