[英]Toggle Text from asterisk to Normal text
I was wondering is it possible to have text starting out as ******
and when they click the eye it goes to 123456
?我想知道是否有可能让文本以
******
开头,当他们点击眼睛时,它会转到123456
?
the number that I want to show/hide is in MySQL, and also what they use to reset their password, so would it also be possible to hide it after like 5-10 seconds.我想显示/隐藏的数字在 MySQL 中,还有他们用来重置密码的数字,所以也可以在 5-10 秒后隐藏它。
Thanks, Tom谢谢,汤姆
You can use <input type="password">
and change the type to "text"
to show the characters visibly.您可以使用
<input type="password">
并将类型更改为"text"
以明显显示字符。
let input = document.querySelector('input'); document.querySelector('button').addEventListener('click', function(e){ if(input.type === 'password') input.type = 'text'; else input.type = 'password'; });
<input type="password" value="123456"> <button> Toggle </button>
If you don't want to use an input field, this is a very quick outline of what you could do:如果您不想使用输入字段,以下是您可以执行的操作的快速概述:
var resetCode = document.getElementById('resetCode'); var showCode = document.getElementById('showCode'); showCode.addEventListener('click', showHiddenText, false); function showHiddenText (e) { var _hidden = resetCode.getAttribute('data-hidden'); // check if the text is hidden so we're not triggering multiple timers if (!!_hidden) { resetCode.setAttribute('data-hidden', 'false'); resetCode.textContent = resetCode.getAttribute('data-value'); // reset the display after an arbitrary amount of time setTimeout(function () { resetCode.setAttribute('data-hidden', 'true'); resetCode.textContent = "******" }, 5000); } }
<!-- data-hidden would be set by default --> <!-- data-value would be obtained from the server via ajax or templating engine --> <span id="resetCode" data-hidden="true" data-value="724814">******</span> <button id="showCode">Show Code</button>
In light of your comment, here's an update:根据您的评论,这里有一个更新:
var _timer; var resetCode = document.getElementById('resetCode'); var showCode = document.getElementById('showCode'); var hideCode = document.getElementById('hideCode'); showCode.addEventListener('click', clickShow, false); hideCode.addEventListener('click', clickHide, false); function clickHide () { var _hidden = resetCode.getAttribute('data-hidden'); if (!!_hidden) { hideTheThings(); clearTimeout(_timer); } } function clickShow () { var _hidden = resetCode.getAttribute('data-hidden'); if (!!_hidden) { showTheThings(); _timer = setTimeout(hideTheThings, 5000); } } function showTheThings () { hideCode.classList.remove('hidden'); showCode.classList.add('hidden'); resetCode.setAttribute('data-hidden', 'false'); resetCode.textContent = resetCode.getAttribute('data-value'); } function hideTheThings () { hideCode.classList.add('hidden'); showCode.classList.remove('hidden'); resetCode.setAttribute('data-hidden', 'true'); resetCode.textContent = "******"; }
.hidden { display: none; }
<!-- data-hidden would be set by default --> <!-- data-value would be obtained from the server via ajax or templating engine --> <span id="resetCode" data-hidden="true" data-value="724814">******</span> <button id="showCode">Show Code</button> <button id="hideCode" class="hidden">Hide Code</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.