VUE-how to change the style by typing the name of it

孤人 提交于 2020-08-10 19:52:27

问题


I tried to change the part of the style attribute by input the number or any details in the text input. However, I want to replace the entire object specifying the style as below. But it only allows me to change the part of the object only.

What whould I do?

html

  <div>
    <label> Type width</label>
    <input type="text" v-on:keydown.enter= 'defst($event.target.value)'>
    <div :style='stylename'>kkk</div>
  </div>

js

new Vue({
  el: '#exercise',
  data: {

    narrow:{
      backgroundColor:'lemonchiffon',
      width:150+'px'
    },
    medium:{
      backgroundColor:'yellow',
      width:250+'px'
    },
    large:{
      backgroundColor:'organge',
      width: 350+'px'
    },
    xlarge:{
      backgroundColor:'red',
      width: 500 +'px'
    },
    stylename:''

  },
  methods: {

  defst(x){
    console.log(x);
    this.stylename=x
  }

}
});

Thank you in advance.


回答1:


change this part:

  defst(x){
    console.log(x);
    this.stylename=x
  }

to

  defst(x){
    console.log(x);
    this.stylename=this[x]
  }


来源:https://stackoverflow.com/questions/61553769/vue-how-to-change-the-style-by-typing-the-name-of-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!