0
点赞
收藏
分享

微信扫一扫

vue 动态的修改样式

JakietYu 2023-05-26 阅读 55


需要为元素配置ref属性

<template>
<!-- 普通的HTML元素 -->
<div class="id_container" ref='id_container'>
    <!-- element-ui框架的元素 -->
    <el-button type='text' ref="elButton">el按钮</el-button>
</div>
</template>

2.在js中进行元素样式的修改,在这里需要区分普通HTML元素还是element-ui框架的元素

  • 普通HTML元素获取

this.$refs.id_container.style.cssText = 'background:green;';

  • element-ui框架元素获取

this.$refs.elButton.$el.style.background='red';

 

  • 在操作时,如果需要写很多的样式,可以使用style.cssText='xxxx',如果是单独设置某一个属性,可以直接使用style.xxx来实现,同时,修改方法需要在钩子函数mouted完全挂载的时候。
举报

相关推荐

0 条评论