vue中通过点击事件修改css样式,两个按钮样式切换

通过ref修改

<div class="sub" ref="abc" @click="submit()">确定</div>
  submit() {
      if (!this.password) {//我写的判断
        this.$refs.abc.style.background = "#ccc";
      } else {
        this.$refs.abc.style.background = "#07C160";
      }
    },

有的时候通过值来判断修改

      <span
        :class="this.state==1?'active ':'active1'"
        @click="deviceEnableOrDisable(0)"
        ref="abc"
      >正常</span>
      <span
        :class="this.state==0?'active ':' active1'"
        style="margin-left:10px;"
        @click="deviceEnableOrDisable(1)"
        ref="abc1"
      >停用</span>

默认state有值的情况下,有基础样式。其次在点击修改(相当于按钮样式切换的笨方法)
deviceEnableOrDisable(value) {
  if (value == 0) {
    this.$refs.abc.style.background = "#52c41a";
    this.$refs.abc.style.color = "#fff";
    this.$refs.abc1.style.background = "transparent";
    this.$refs.abc1.style.color = "#1890ff";
  } else if (value == 1) {
    this.$refs.abc1.style.background = "#52c41a";
    this.$refs.abc1.style.color = "#fff";
    this.$refs.abc.style.background = "transparent";
    this.$refs.abc.style.color = "#1890ff";
  }
},
  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过一个按钮切换两种全局样式,您可以使用uni-app提供的全局变量和计算属性来实现。下面是一个示例: 1. 在App.vue文件定义全局变量`theme`,用来记录当前的主题,以及两种主题的样式: ```html <template> <div :class="theme"> <!-- 页面内容 --> <router-view></router-view> </div> </template> <script> export default { name: 'App', data() { return { theme: 'light' // 默认主题为light } }, computed: { themeStyles() { if (this.theme === 'light') { return { backgroundColor: '#fff', color: '#333' } } else { return { backgroundColor: '#333', color: '#fff' } } } } } </script> <style> /* 全局CSS样式 */ .light button { background-color: #ccc; color: #333; } .dark button { background-color: #333; color: #ccc; } </style> ``` 在上面的示例,我们定义了一个`theme`变量,用来记录当前的主题。当`theme`为`light`时,应用`light`主题的样式,当`theme`为`dark`时,应用`dark`主题的样式。同时,我们还定义了一个计算属性`themeStyles`,用来返回当前主题的样式。 2. 在需要切换主题的页面添加一个按钮,并通过点击事件切换主题: ```html <template> <div> <!-- 页面内容 --> <button @click="toggleTheme">切换主题</button> </div> </template> <script> export default { name: 'Page', methods: { toggleTheme() { if (this.$root.theme === 'light') { this.$root.theme = 'dark' } else { this.$root.theme = 'light' } } } } </script> <style> /* 页面CSS样式 */ button { border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; } </style> ``` 在上面的示例,我们添加了一个按钮,并通过点击事件切换主题。在点击事件,我们通过访问`this.$root.theme`来获取和修改全局变量`theme`的值。当`theme`的值为`light`时,切换为`dark`主题,当`theme`的值为`dark`时,切换为`light`主题。 通过以上步骤,您就可以通过一个按钮切换两种不同的全局样式了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值