gpt4 book ai didi

javascript - 如何调用函数 onclick,同时还更改函数外部文本的值

转载 作者:行者123 更新时间:2023-12-05 02:28:38 24 4
gpt4 key购买 nike

长话短说,我正在尝试为一个项目进行密码强度测试,但我想如果我不能在点击时接受输入然后将其放入我的函数中,我将无法到达任何地方测试并发回。

但基本上第一步我就被难住了。

我现在的目标是让火车开动,能够点击按钮,然后将中间的大强度文本更改为“它起作用了”或类似的东西,找到一种方法来调用该函数并正确使用它。

(仅供引用,我正在制作一个密码生成器,就像二合一交易一样,这就是为什么有一个额外的按钮并且 css 文件有一些“冗余”代码)

非常感谢任何帮助<3

/* Background color + text colour and centering */
body {
background-color: black;
color: black;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0%;
font-family: Arial, Helvetica, sans-serif;
}
/* div block */
.container {
background-color: yellow;
padding: 3rem;
border: 2px solid white;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
/* spacing out text and inputs */
.form {
display: grid;
grid-template-columns: auto auto;
row-gap: 1rem;
column-gap: 3rem;
justify-content: center;
align-items: center;
}

.title {
text-align: center;

}

/* Making text easier to see/read */
label {
font-weight: bold;
}
.characteramountrange {
display: flex;
}

.number-input {
width: 2rem;
}

/*display box*/
.password-display {
background-color: white;
color: black;
padding: 1rem;
border: 1px solid black;
height: 2rem;
width: 350px;
display: flex;
justify-content: center;
align-items: center;
word-break: break-all;
}
/* Customizing the style of the "Generate" Button */
.button {
grid-column: span 2;
background-color: white;
border: 2px solid black;
color: black;
padding: .5rem 1rem;
font-weight: bold;
cursor: pointer;
}

.button:hover {
background-color: #FFFFFF33;
}
/* redirect button */
.btn {
background-color: white;
border: 2px solid black;
color: black;
font-weight: bold;
cursor: pointer;
padding: 5px;
}
.btn:hover {
background-color: #FFFFFF33;
}
   
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>Password Strength Test</title>
</head>
<body>
<div class="container">
<h1 class="title">Password Strength Test</h1>
<input type="text" class= "password" value= "password" id="password">
<form id="passwordGeneratorForm" class="form">
<h1 id="strength">Strength</h1>
<button type="button" class="button" onclick="PasswordTest()" id="btn">Check Strength</button>
<a href="index.html" class="btn" style="text-decoration: none;">Password Generator</a>
</form>
<script>
function PasswordTest() {
document.getElementById("strength")
let strength = "it worked"
return strength;
}
</script>
</section>
</div>
</body>
</html>

最佳答案

要使您的代码正常工作,请更改此行:

let strength = "it worked"

到:

strength.innerHTML = "it worked"

要更改 html 元素的内容,您需要设置 .innerHTML 或 .innerText 属性。或者,如果它是一个表单控件,如输入或选择,那么您将设置 .value。由于您已将元素的 ID 设置为强度,因此您可以使用它来引用该元素。和 strength.innerHTML = “它成功了!”然后将更改屏幕上显示的内容。

运行代码片段以查看其工作原理。

function PasswordTest() {
strength.innerHTML = "it worked"
}
<input type="text" class="password" placeholder="password" id="password">
<h1 id="strength">Strength</h1>
<button type="button" class="button" onclick="PasswordTest()" id="btn">Check Strength</button>

关于javascript - 如何调用函数 onclick,同时还更改函数外部文本的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72567517/

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