vue 如何动态设置 input 的 placeholder 的颜色?

如题,vue如果动态设置input的颜色,网上查找的方法

  this.$refs.xxxx.style.setProperty('color', 'red')

不起效果

阅读 3.2k
2 个回答

刚才经过测试找到了解决的方案:

// demo.vue
<input
      type="text"
      placeholder="输入内容"
      :style="bindInputStyle"
    />
...
computed:{
   bindInputStyle() {
        return {
          '--placeholderColor': 'red',
        }
      },
}
...

<style scoped lang="scss">
input {
  &::placeholder {
      color: var(--placeholderColor); // 动态值
    }
}
</style>

参考链接:

css的var的兼容

先试下:this.$refs.xxxx 是不是选中了当前 input dom元素。
答案是是的话,再试试 this.$refs.xxxx.style.color = 'red'
然后检查下 Element 面板,查看当前 input dom的样式是否生效。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题