- 問題
小程序的input組件有個自身的bug,即當輸入框獲取焦點時placeholder內容會出現重影現象。 - 解決思路
原理:將placeholder內容單獨寫在另外的標簽里,控制其顯示隱藏。
操作:將代表placeholder內容的標簽定位到input框上,在input輸入事件中控制標簽隱藏,在input失焦事件中若輸入框內容為空時控制標簽顯示。 - 代碼實現
// wxml文件 <view> <input placeholder='' value="{{username}}" bindinput="handleName" bindblur="blurAccount"></input> <text class="placeholder" wx:if="{{placeAccount}}">賬號</text> </view> // js文件 data: {placeAccount: true}, handleName() { // input輸入事件 this.setData({ placeAccount: false }) }, blurAccount() { // input失焦事件 if (!this.data.username) { this.setData({ placeAccount: true }) } }