gpt4 book ai didi

javascript - 我的 addEventListner 在 javascript 中返回 null 的问题

转载 作者:行者123 更新时间:2023-11-30 10:59:18 24 4
gpt4 key购买 nike

嘿,想知道是否有人能帮我找出我在这段代码中出错的地方。我对编码很陌生,如果有点不稳定,我很抱歉。非常乐于接受任何反馈!我正在尝试制作一个密码生成器,提示您询问总共有多少个字符以及要包含哪些字符。

这个函数在我记录它时有效,但是将它添加到按钮以在您单击它时生成它有点走下坡路。

也不是那么重要,但在我的提示中,如果他们在参数之外放置一个数字,我无法弄清楚如何让 else 语句重新启动该过程。

var spec = '!@$%^&*()_+';
var low = 'abcdefghijklmnopqrstuvwxyz';
var num = '0123456789';
var upp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var charLength = prompt("How many characters between 8 - 128?");

if (charLength >= 8, charLength <= 128) {
var lowChar = confirm("Include lowercase letters?");
var upChar = confirm("Include uppercase letters?");
var specChar = confirm("Include special characters?");
var numChar = confirm("Include numbers?");
}

else {
var charLength = prompt("How many characters between 8 - 128?");
};
var passwords = document.getElementById("password");
var generate = document.getElementById("btnGen");
var copy = document.getElementById("btnCopy");

generate.addEventListener("click", function(){
var characters = '';
lowChar ? characters += low : '';
upChar ? characters += upp : '';
specChar ? characters += spec : '';
numChar ? characters += num : '';

passwords.value = password(charLength.value, characters)
});


function password(l, characters){
var pwd = '';
for(var i = 0; i < l; i++){
pwd += characters.charAt(Math.floor(Math.random() * characters.length))
}
return pwd;
}

谢谢!

最佳答案

eventListener函数有两个错误请检查新代码。你拼错了“var character”而不是“var characters”。当调用“密码”函数时,第一个参数必须是 charLength 而不是 charLength.value。

generate.addEventListener("click", function(){
var characters = ''; /* var character = ''; */
lowChar ? characters += low : '';
upChar ? characters += upp : '';
specChar ? characters += spec : '';
numChar ? characters += num : '';
passwords.value = password(charLength /*charLength.value*/, characters)
});

var spec = '!@$%^&*()_+';
var low = 'abcdefghijklmnopqrstuvwxyz';
var num = '0123456789';
var upp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var charLength = prompt("How many characters between 8 - 128?");

if (charLength >= 8, charLength <= 128) {
var lowChar = confirm("Include lowercase letters?");
var upChar = confirm("Include uppercase letters?");
var specChar = confirm("Include special characters?");
var numChar = confirm("Include numbers?");
}

else {
var charLength = prompt("How many characters between 8 - 128?");
};
var passwords = document.getElementById("password");
var generate = document.getElementById("btnGen");
var copy = document.getElementById("btnCopy");

generate.addEventListener("click", function(){
var characters = '';
lowChar ? characters += low : '';
upChar ? characters += upp : '';
specChar ? characters += spec : '';
numChar ? characters += num : '';
passwords.value = password(charLength, characters)
});


function password(l, characters){
var pwd = '';
for(var i = 0; i < l; i++){
pwd += characters.charAt(Math.floor(Math.random() * characters.length))
}
return pwd;
}
<html>
<head>

</head>
<body>
<input type="text" id="password"/>

<div>


<span id="btnGen">Generate</span>
<span id="btnCopy">Copy</span>
</div>
</body>
</html>

关于javascript - 我的 addEventListner 在 javascript 中返回 null 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58567831/

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