gpt4 book ai didi

javascript - 显示隐藏密码

转载 作者:行者123 更新时间:2023-12-04 14:04:58 33 4
gpt4 key购买 nike

我目前正在学习 JavaScript。当我有一个输入时它可以工作,但是当我有多个输入时我对循环遍历图标和输入感到困惑。你认为是什么问题?对不起,我的英语不好。我希望你明白我在说什么

const inputs = document.querySelectorAll('.show-hide-password');
const icon = document.querySelectorAll('i.password');

// Experiment 1
icon.forEach(function (ele) {
ele.addEventListener('click', function (e) {
const targetInput = e.target.previousElementSibling.getAttribute('type');
if (targetInput == 'password') {
targetInput.type = 'text';
ele.classList.remove('fa-eye-slash');
ele.classList.add('fa-eye');
} else if (targetInput == 'text') {
targetInput.type = 'password';
ele.classList.add('fa-eye-slash');
ele.classList.remove('fa-eye');
}
});
});

// Experiment 2
// icon.forEach(function (ele) {
// ele.addEventListener('click', function (e) {
// inputs.forEach(function (input) {
// const targetInput = input.getAttribute('type');
// if (targetInput == 'password') {
// targetInput.type = 'text';
// ele.classList.remove('fa-eye-slash');
// ele.classList.add('fa-eye');
// } else if (targetInput == 'text') {
// targetInput.type = 'password';
// ele.classList.add('fa-eye-slash');
// ele.classList.remove('fa-eye');
// }
// });
// });
//});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="form-group">
<label>New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
<div class="form-group">
<label>Confirm New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>

最佳答案

您必须使用 Element.setAttribute 更改元素属性像这样:

const inputs = document.querySelectorAll('.show-hide-password');
const icon = document.querySelectorAll('i.password');

// Experiment 1
icon.forEach(function (ele) {
ele.addEventListener('click', function (e) {
const targetInput = e.target.previousElementSibling.getAttribute('type');
if (targetInput == 'password') {
e.target.previousElementSibling.setAttribute('type', 'text');
ele.classList.remove('fa-eye-slash');
ele.classList.add('fa-eye');
} else if (targetInput == 'text') {
e.target.previousElementSibling.setAttribute('type', 'password');
ele.classList.add('fa-eye-slash');
ele.classList.remove('fa-eye');
}
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="form-group">
<label>New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>
<div class="form-group">
<label>Confirm New Password</label>
<input type="password" class="form-control show-hide-password" autocomplete="off" required>
<i class="fa fa-eye-slash password"></i>
</div>

关于javascript - 显示隐藏密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68878865/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com