el-input中show-password密码提示功能去掉

ops/2024/9/23 14:24:46/

el-input中show-password密码提示功能去掉

  • 一、效果图
  • 二、封装个组件
  • 三、如何使用

一、效果图

在这里插入图片描述

二、封装个组件

javascript"><template><divclass="el-password el-input":class="[size ? 'el-input--' + size : '', { 'is-disabled': disabled }]"><inputclass="el-input__inner":placeholder="placeholder"ref="input":style="{ paddingRight: padding + 'px' }":disabled="disabled":readonly="readonly"@focus="handleFocus"@blur="handleBlur"@input="handleInput"@change="change"@compositionstart="handleCompositionStart"@compositionend="handleCompositionEnd"/><div class="tools"><iv-if="clearable !== false"v-show="pwd !== '' && isfocus"@mousedown.preventclass="el-input__icon el-icon-circle-close el-input__clear"@click="clearValue"></i><iv-if="showPassword !== false"v-show="pwd !== '' || isfocus"class="el-input__icon el-icon-view el-input__clear"@click="changePasswordShow"></i></div></div>
</template>
<script>
import emitter from 'element-ui/src/mixins/emitter';
//自定义密码输入框//input元素光标操作
class CursorPosition {constructor(_inputEl) {this._inputEl = _inputEl;}//获取光标的位置 前,后,以及中间字符get() {var rangeData = { text: '', start: 0, end: 0 };if (this._inputEl.setSelectionRange) {// W3Cthis._inputEl.focus();rangeData.start = this._inputEl.selectionStart;rangeData.end = this._inputEl.selectionEnd;rangeData.text =rangeData.start != rangeData.end? this._inputEl.value.substring(rangeData.start, rangeData.end): '';} else if (document.selection) {// IEthis._inputEl.focus();var i,oS = document.selection.createRange(),oR = document.body.createTextRange();oR.moveToElementText(this._inputEl);rangeData.text = oS.text;rangeData.bookmark = oS.getBookmark();for (i = 0;oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart('character', -1) !== 0;i++) {if (this._inputEl.value.charAt(i) == '\r') {i++;}}rangeData.start = i;rangeData.end = rangeData.text.length + rangeData.start;}return rangeData;}//写入光标的位置set(rangeData) {var oR;if (!rangeData) {console.warn('You must get cursor position first.');}this._inputEl.focus();if (this._inputEl.setSelectionRange) {//  W3Cthis._inputEl.setSelectionRange(rangeData.start, rangeData.end);} else if (this._inputEl.createTextRange) {// IEoR = this._inputEl.createTextRange();if (this._inputEl.value.length === rangeData.start) {oR.collapse(false);oR.select();} else {oR.moveToBookmark(rangeData.bookmark);oR.select();}}}
}
export default {name: 'el-password',props: {value: { default: '' },size: { type: String, default: '' },placeholder: { type: String, default: '请输入' },disabled: { type: [Boolean, String], default: false },readonly: { type: [Boolean, String], default: false },clearable: { type: [Boolean, String], default: false },showPassword: { type: [Boolean, String], default: false },},data() {return {symbol: '●', //自定义的密码符号pwd: '', //密码明文数据padding: 15,show: false,isfocus: false,inputEl: null, //input元素isComposing: false, //输入框是否还在输入(记录输入框输入的是虚拟文本还是已确定文本)};},mounted() {this.inputEl = this.$refs.input;this.pwd = this.value;this.inputDataConversion(this.pwd);},mixins: [emitter],watch: {value: {handler: function (value) {if (this.inputEl) {this.pwd = value;this.inputDataConversion(this.pwd);}},},showPassword: {handler: function (value) {let padding = 15;if (value) {padding += 18;}if (this.clearable) {padding += 18;}this.padding = padding;},immediate: true,},clearable: {handler: function (value) {let padding = 15;if (value) {padding += 18;}if (this.showPassword) {padding += 18;}this.padding = padding;},immediate: true,},},methods: {select() {this.$refs.input.select();},focus() {this.$refs.input.focus();},blur() {this.$refs.input.blur();},handleFocus(event) {this.isfocus = true;this.$emit('focus', event);},handleBlur(event) {this.isfocus = false;this.$emit('blur', event);//校验表单this.dispatch('ElFormItem', 'el.form.blur', [this.value]);},change(...args) {this.$emit('change', ...args);},clearValue() {this.pwd = '';this.inputEl.value = '';this.$emit('input', '');this.$emit('change', '');this.$emit('clear');this.$refs.input.focus();},changePasswordShow() {this.show = !this.show;this.inputDataConversion(this.pwd);this.$refs.input.focus();},inputDataConversion(value) {//输入框里的数据转换,将123转为●●●if (!value) {this.inputEl.value = '';return;}let data = '';for (let i = 0; i < value.length; i++) {data += this.symbol;}//使用元素的dataset属性来存储和访问自定义数据-*属性 (存储转换前数据)this.inputEl.dataset.value = this.pwd;this.inputEl.value = this.show ? this.pwd : data;},pwdSetData(positionIndex, value) {//写入原始数据let _pwd = value.split(this.symbol).join('');if (_pwd) {let index = this.pwd.length - (value.length - positionIndex.end);this.pwd =this.pwd.slice(0, positionIndex.end - _pwd.length) + _pwd + this.pwd.slice(index);} else {this.pwd =this.pwd.slice(0, positionIndex.end) +this.pwd.slice(positionIndex.end + this.pwd.length - value.length);}},handleInput(e) {//输入值变化后执行 //撰写期间不应发出输入if (this.isComposing) return;let cursorPosition = new CursorPosition(this.inputEl);let positionIndex = cursorPosition.get();let value = e.target.value;//整个输入框的值if (this.show) {this.pwd = value;} else {this.pwdSetData(positionIndex, value);this.inputDataConversion(value);}cursorPosition.set(positionIndex, this.inputEl);this.$emit('input', this.pwd);},handleCompositionStart() {//表示正在写this.isComposing = true;},handleCompositionEnd(e) {if (this.isComposing) {this.isComposing = false;//handleCompositionEnd比handleInput后执行,避免isComposing还为true时handleInput无法执行正确逻辑this.handleInput(e);}},},
};
</script>
<style scoped>
.tools {position: absolute;right: 5px;display: flex;align-items: center;top: 0;height: 100%;z-index: 1;
}
</style>

三、如何使用

javascript">// An highlighted block
import elPassword from '@/components/elPassword.vue';components: {elPassword,
},<el-passwordshow-passwordv-model="formData.password1"placeholder="新密码"
></el-password>

结束啦~~~~
在这里插入图片描述

链接: https://www.jb51.net/javascript/318001fxj.htm


http://www.ppmy.cn/ops/100511.html

相关文章

【区块链 + 司法存证】区块链电子数据存证平台 | FISCO BCOS应用案例

基于 FISCO BCOS 技术的区块链电子数据存证平台被广泛应用于金融、司法、政务、电商、能源、医疗等领域&#xff0c; 利用区块链多中心化、多方共识、不可篡改、全程留痕等特性&#xff0c;将业务办理过程中产生的身份信息、资产、行为 上链&#xff0c;从而保证所存证据的真实…

卖旧电脑前怎么彻底清除数据?卖旧电脑不留隐患

在科技日新月异的今天&#xff0c;电脑已成为我们日常生活和工作中不可或缺的工具。然而&#xff0c;随着技术的不断进步&#xff0c;我们可能会考虑更换新的电脑设备&#xff0c;而将旧的电脑出售或转让。 在卖旧电脑前&#xff0c;彻底清除电脑中的数据至关重要&#xff0c;…

神经网络——最大池化

1.Pooling Layers讲解&#xff1a; 最大池化有时也被称为下采样&#xff0c;对应的有上采样。注意ceil_mode参数的使用 2.代码实战&#xff1a; import torch from torch import nn from torch.nn import MaxPool2dinputtorch.tensor([[1,2,0,3,1],[0,1,2,3,1],[1,2,1,0,0],…

OpenCV几何图像变换(11)极坐标转换函数warpPolar()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 函数将图像重映射到极坐标或半对数极坐标空间。 极坐标重映射参考 用以下转换来转换源图像&#xff1a; d s t ( ρ , ϕ ) s r c ( x , y ) ds…

行业智能化的“火车头效应”,由星河AI金融网络启动

相信大多数人都认可&#xff0c;在行业智能化的列车中&#xff0c;金融是毋庸置疑的“火车头”。 有数据显示&#xff0c;目前AI整体渗透率只有4%&#xff0c;不同行业的AI渗透度有极大差异。其中&#xff0c;金融由于数字基础好&#xff0c;拥抱新技术的意愿强烈&#xff0c;成…

开发日志:表单解析 LeipiFormDesign

PHP版本&#xff1a;https://gitee.com/yxkj_2/LeipiFormDesigner/blob/LeipiFormDesigner/Formdesign4_1/php/Formdesign.class.php js版本&#xff1a; var leipiFormDesign {/*执行控件*/exec: function (method) {ue.execCommand(method);},/*Javascript 解析表单templat…

【云原生】Kubernetes中常见的Pod故障排查定位与解决方案

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全…

C语言 | Leetcode C语言题解之第372题超级次方

题目&#xff1a; 题解&#xff1a; //计算a的b次方 int mypow(int a, int b){a a % 1337; // 防止a过大超出规模int ret 1;for(int i 0; i < b; i){ret * a;ret ret % 1337; //防止超出规模}return ret; } //整体计算 int superPow(int a, int* b, int bSize){if(a 1…