Vue.JS value tied on input having the focus

前端 未结 4 1610
孤街浪徒
孤街浪徒 2021-01-31 14:20

Is there a way to change a value in the model when an input gets/loses focus?

The use case here is a search input that shows results as you type, these should only show

4条回答
  •  面向向阳花
    2021-01-31 15:09

    You can use a flat by determinate a special CSS class, for example this a simple snippet:

    var vm = new Vue({
    	el: '#app',
    	data: {
    		content: 'click to change content',
    		flat_input_active: false
    	},
    	methods: {
    		onFocus: function(event) {
    	    	    event.target.select();
    	    	    this.flat_input_active = true;
    		},
    		onBlur: function(event) {
    	    	    this.flat_input_active = false;
    		}
    	},
    	computed: {
    		clazz: function() {
    			var clzz = 'control-form';
    			if (this.flat_input_active == false) {
    				clzz += ' only-text';
    			}
    			return clzz;
    		}
    	}
    });
    #app {
      background: #EEE;
    }
    input.only-text { /* special css class */
      border: none;
      background: none;
    }
    
    
    
    
    
    

    Good luck

提交回复
热议问题