vue通过js修改元素的样式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Frankenstein_/article/details/84582740
  • 1 给元素定义ref 属性
 <el-button ref="btnClick" class="list_button" " @click="openClose"></el-button>
  •  2 通过js 方法修改元素的样式   修改较多样式时可使用cssText
function openClose() {
        this.isCollapse = !this.isCollapse

        if (this.isCollapse) {
            this.$refs.btnClick.$el.style.cssText =
                'background-color:#1F2429;width:66px';
           
        } else {
            this.$refs.btnClick.$el.style.cssText =
                'background-color:#1F2429;width:140px';
        }
    }
  • 3  修改单一样式时可直接使用样式名称
function openClose() {
        this.isCollapse = !this.isCollapse

        if (this.isCollapse) {
            this.$refs.btnClick.$el.style.color = 'red';
           
        } else {
            this.$refs.btnClick.$el.style.color = 'blue';
        }
    }

猜你喜欢

转载自blog.csdn.net/Frankenstein_/article/details/84582740