gpt4 book ai didi

像退格键一样的javascript子字符串

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:08 26 4
gpt4 key购买 nike

嗨,在下面的代码中,我有一个带有密码输入表单按钮的 html 设置:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Log in form</title>
<link href="style.css" rel="stylesheet">
<script src="ui.js"></script>
</head>
<body>
<div id="main">
<div id="top_bar"><tx id="tx">Bildschirmsperre</tx></div>
<div id="infotext">Wählen sie Ihre PIN aus</div><br />
<div id="pin"></div><button id="btnback" onclick="changepin(10)">&larr;</button><br />

<div id="tasten">
<button class="button" onclick="changepin(1)">1</button>
<button class="button" onclick="changepin(2)">2</button>
<button class="button" onclick="changepin(3)">3</button><br />
<button class="button" onclick="changepin(4)">4</button>
<button class="button" onclick="changepin(5)">5</button>
<button class="button" onclick="changepin(6)">6</button><br />
<button class="button" onclick="changepin(7)">7</button>
<button class="button" onclick="changepin(8)">8</button>
<button class="button" onclick="changepin(9)">9</button><br />
<button class="button" onclick="changepin(0)">0</button>
</div>
<div id="bottom_bar"> <text id="text_left">ABBRECHEN</text> <zeichen id="zeichen">|</zeichen> <text id="text_right">WEITER</text> </div>
</div>
</body>
</html>

没有 css,所以它现在看起来很糟糕,而且它包含德语单词,所以请忽略它们。

到目前为止我有这个javascript:

var count = 0;

function changepin(value) {
var pin = document.getElementById("pin");
if(value != 10 && count < 4){
pin.innerHTML += value;
count++;
}else if(value == 10){
count--;
pin.innerHTML = pin.value.substr(0, count);
}
}

输入(最多 4 个字母)工作正常,但我不知道如何使退格键我尝试了一点,但它不起作用。

有人可以帮忙吗?

谢谢!

最佳答案

您几乎已经做到了,但是 count 变量的使用是不必要的,而且可能会造成混淆。没有它,这可以满足您的需求...

// only do this once - no need to find the element every time
var pin = document.getElementById("pin");

function changepin(value) {
var currentPin = pin.innerText;

// if user pressed the back button then remove the last character
if (value == 10) {
pin.innerText = currentPin.substr(0, currentPin.length - 1);
}
// else if the current pin is long enough then just exit without doing anything else
else if (currentPin.length > 3) {
return;
}
// else add the new character to the pin
else {
pin.innerText = currentPin + value;
}
}

如您所见,我还将 innerHTML 更改为 innerText。在这种情况下没有区别,但始终引用正确的属性是个好习惯,因此您要设置文本,而不是 HTML。

关于像退格键一样的javascript子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38700965/

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