效果图:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generate Random Four-Digit Verification Code</title>
</head>
<body><h1>点击生成四位随机数</h1><input type="text" id="verificationInput"><button id="generateCode">Generate Code</button><script>const generateCodeButton = document.getElementById("generateCode");const verificationInput = document.getElementById("verificationInput");let randomCode = '';generateCodeButton.addEventListener('click', function() {randomCode = Math.floor(1000 + Math.random() * 9000); // Generate a random four-digit numberverificationInput.value = randomCode;});verificationInput.addEventListener('click', function() {verificationInput.value = randomCode;});</script>
</body>
</html>
当前效果是需要点击按钮,也可以写在点击按钮里面,根据需求适当调整;
希望可以帮到你们;