這是某盟的一道面試題
難道不都是隻要有了清晰的思路後邊寫邊優化麼
當時我就說了思路用計算屬性根據輸入框的內容計算出預覽框的值
處理focus和blur事件便可
非要手寫,手寫根本寫不出啊,而後面試官就認爲我不會 無語了html
就是用計算屬性判斷便可vue
<template lang="html"> <div class="zInput"> <div class="big-show" v-show="showBig">{{txt2}}</div> <input type="text" @blur="handerBlur" @focus="handerFocus" v-model="txt"> </div> </template> <script> export default { name: 'z-input', data () { return { txt: '', showBig: false, } }, computed: { txt2: function () { if (this.txt.length > 0) { this.showBig = true } return this.getStr(this.txt, 4) } }, methods: { handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解決最後一位爲補充項問題 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script> <style lang="less"> .zInput{ position: relative; .big-show { position: absolute; top: -40px; font-size: 36px; line-height: 40px; background-color: red; } } .zInput{ top:50px; } </style>
若是再加入個長度限制,則以上方法就不合適了,更換實現方案v-model
實際上是個語法糖
分開寫爲v-bind:value
和v-on:input
面試
<template lang="html"> <div class="zInput"> <div class="big-show" v-show="showBig">{{txt2}}</div> <input type="text" @blur="handerBlur" @focus="handerFocus" v-bind:value="txt" v-on:input="handerInput"> </div> </template> <script> export default { name: 'z-input', data () { return { txt: '', txt2: '', showBig: false, } }, methods: { handerInput (evt) { let val = evt.target.value.substr(0, 13) // 限制長度13位 this.txt = val evt.target.value = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解決最後一位爲補充項問題 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script>
限制只能輸入數字
首先想到的是在keyup
時把非數字過濾掉
可是事實是先執行keydown
->handerInput
->keyup
less
那就在keydown
時處理唄,可是keydown修改evt.target.value
後
在handerInput
取到的evt.target.value
依舊是未處理的因此說在keydown
處理不起做用
必需要在handerInput
時處理ide
<input type="text" @blur="handerBlur" @focus="handerFocus" @keyup="handerKeyup" @keydown="handerKeydown" v-bind:value="txt" v-on:input="handerInput"> <script> handerKeydown (evt) { console.log('handerKeydown') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 這裏修改不起做用 }, handerKeyup (evt) { console.log('keyup') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 這裏執行順序靠後 修改無用 }, handerInput (evt) { let val = evt.target.value.substr(0, 13).replace(/[^\d]/g, '') console.log('handerInput__val', val) //這裏拿到的val是純數字 evt.target.value = val this.txt = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, </script>