Vue.js 实现动态修改带icon的input样式

文章讨论了在Vue.js应用中,尝试通过JavaScript直接修改el-input组件及其子元素span的样式存在的问题,即样式在页面刷新后失效。作者提出了解决方案,即通过全局SCSS样式而非JS动态修改,以确保样式持久生效。
摘要由CSDN通过智能技术生成

<el-input

            v-model.trim="loginForm.password"

            type="password"

            auto-complete="off"

            placeholder="秘钥"

            show-password

            ref ="indexInputps"

          >

            <svg-icon

              slot="prefix"

              icon-class="password"

              class="el-input__icon input-icon"

            />

          </el-input>

mounted() {

console.log("this.$refs.indexInput",this.$refs.indexInputcd.$el.querySelector('span'));

    console.log("this.$refs.indexInput.$refs",this.$refs.indexInputcd.$refs.input);

    const prefixcd = this.$refs.indexInputcd.$el.querySelector('span'); // el-input__prefix的DOM元素

    const prefixps = this.$refs.indexInputps.$el.querySelector('span');

    const prefixuc = this.$refs.indexInputuc.$el.querySelector('span');

    const innercd = this.$refs.indexInputcd.$refs.input; // el-input__inner的DOM元素

    const innerps = this.$refs.indexInputps.$refs.input; // el-input__inner的DOM元素

    const inneruc = this.$refs.indexInputuc.$refs.input; // el-input__inner的DOM元素

   //prefix.style={'backgroud':'#f0d29d !important','line-height':'48px  !important','left':'1px  !important','color':'#f0d29d !important'}; // 添加类名,修改样式

    const numh = "48px";

    const classT = "#f0d29d";

    const leftl = "1px";

    innercd.style.paddingLeft=numh;

    innercd.style.height=numh;

    innercd.style.lineHeight=numh;

    prefixcd.style.lineHeight=numh;

    prefixcd.style.background=classT;

    console.log("prefixcd.style.cssText",prefixcd.style.cssText);

    prefixcd.style.left=leftl;

    innerps.style.paddingLeft=numh;

    innerps.style.height=numh;

    innerps.style.lineHeight=numh;

    prefixps.style.lineHeight=numh;

    prefixps.style.background=classT;

    prefixps.style.left=leftl;

    inneruc.style.paddingLeft=numh;

    inneruc.style.height=numh;

    inneruc.style.lineHeight=numh;

    prefixuc.style.lineHeight=numh;

    prefixuc.style.background=classT;

    prefixuc.style.left=leftl;

}

1、这个方式有个缺陷,无法修改js生成的标签比如上面的input下的span标签样式是无效的,因为做了唯一处理,二次刷新就无效

另外一个解决方案:

<style lang="scss"> // 注意这里不要放 scoped ,不做唯一处理,其中 .login_index  form加了一层

  .login_index .el-input__prefix {

    background: #f0d29d;

    left:1px;

    line-height: 48px;

  }

  .login_index .el-input__inner {

    padding-left: 48px;

    height:48px;

    line-height: 48px;

  }

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慧香一格

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值