当前位置: 代码网 > it编程>编程语言>Javascript > el-input 密码自动填充的方法汇总

el-input 密码自动填充的方法汇总

2024年09月08日 Javascript 我要评论
避免 el-input 密码自动填充的实用方法在开发 web 应用时,通常需要避免浏览器自动填充密码。以下是一些可行的解决方案,特别针对使用 element ui 框架的 el-input 组件。方法

避免 el-input 密码自动填充的实用方法

在开发 web 应用时,通常需要避免浏览器自动填充密码。以下是一些可行的解决方案,特别针对使用 element ui 框架的 el-input 组件。

方法 1:设置随机的 name 和 autocomplete 属性

浏览器根据 name 属性来识别输入字段的类型,因此可以使用随机的 name 属性,并将 autocomplete 设置为 new-password

实现

<el-input
  type="password"
  :name="randomname"
  autocomplete="new-password"
  v-model="password">
</el-input>
export default {
  data() {
    return {
      password: '',
      randomname: `password_${math.random().tostring(36).substr(2, 9)}`
    };
  }
}

方法 2:使用隐藏的密码输入字段

通过在页面中添加一个隐藏的输入字段,可以避免自动填充密码字段。

实现

<el-input
  type="text"
  style="display: none;"
  autocomplete="username">
</el-input>
<el-input
  type="password"
  autocomplete="new-password"
  v-model="password">
</el-input>

方法 3:使用 meta 标签阻止密码管理器

在 html 中添加以下 meta 标签,可能会阻止某些密码管理器的自动填充功能。

实现

<meta name="disable-autofill" content="on">

方法 4:事件拦截

通过监听输入事件,可以在获取焦点时手动清除输入字段的内容。

实现

methods: {
  clearinput(event) {
    event.target.value = '';
  }
}
<el-input
  type="password"
  autocomplete="new-password"
  @focus="clearinput"
  v-model="password">
</el-input>

方法 5:动态改变 readonly 属性

通过设置 readonly 属性为true,可以避免一开始自动填充,在 mousedown 或 focus 事件触发时设置为 false ,允许输入

实现

          <el-input
            placeholder="密码"
            type="password"
            v-model="loginform.userpwd"
            show-password
            @focus="passwordmousedownfun"
            @input="passwordinputfun"
            @mousedown.native="passwordmousedownfun"
            :readonly="passwordreadonly"
            id="passwordref"
          >
      loginform: {
        username: "",
        userpwd: "",
        userrember: false,
      },
      passwordreadonly: true,
  watch: {
    loginform: {
      handler: function (newvalue, oldvalue) {
        if (newvalue.userpwd == "") {
          this.passwordreadonly = true;
          settimeout(() => {
            this.passwordreadonly = false;
          }, 0);
        }
      },
      deep: true,
    },
  },
    passwordmousedownfun() {
      if (this.loginform.userpwd === "") {
        this.passwordreadonly = true;
      } else {
        if (this.loginform.userpwd == this.originpwd) {
          this.loginform.userpwd = "";
        }
      }
      settimeout(() => {
        this.passwordreadonly = false;
      }, 0);
    },

到此这篇关于el-input 密码自动填充的实用方法的文章就介绍到这了,更多相关el-input自动填充内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com