gpt4 book ai didi

javascript - 为什么 innerHTML 不起作用?

转载 作者:行者123 更新时间:2023-11-30 11:25:21 25 4
gpt4 key购买 nike

我试图使用 innerHTML 更改带有 id 内容的 div 元素,但它只是在登录时闪烁欢迎,然后消失。我知道这很愚蠢,但不明白为什么我会收到此错误。代码如下:

<html>
<head>
<meta charset = "utf-8">
<link rel="stylesheet" href="signup.css">
</head>
<body>
<div id="content" style = "float: left;"></div>
<div class = "login">
<div class = "icon">
<img src="people.png" style ="width:50%; margin: 5% 25% 25% 25%;">
<form>
</br>
<label>User name:</label>
<input type = "text" id = "username" placeholder = "Enter username">
</br></br></br></br>
<label>Password:</label>
<input type = "password" id = "password" placeholder = "Enter Password">
</br></br></br></br>
<input type = "submit" onclick ="return validate()" value ="Login">
</form>
</div>
</div>
</body>
<script type = "text/javascript">

function validate(){
if(document.getElementById("username").value=="Tushar"&&document.getElementById("password").value=="tushar"){
alert("Hello");
document.getElementById("content").innerHTML = '<h1 id = "welcome" style="font-size: 25px;color:#ffffff; ">Welcome</h1>';

}
else{
alert("Invalid username/password");
}
}
</script>

但是欢迎不显示

最佳答案

请使用下面提到的代码..即更改颜色:#ffffff;到其他颜色,因为你使用白色,它是不可见的

<html>
<head>
<meta charset = "utf-8">
<link rel="stylesheet" href="signup.css">
</head>
<body>
<div id="content" style = "float: left;"></div>
<div class = "login">
<div class = "icon">
<img src="people.png" style ="width:50%; margin: 5% 25% 25% 25%;">
<form>
</br>
<label>User name:</label>
<input type = "text" id ="username" placeholder="Enter username"/>
</br></br></br></br>
<label>Password:</label>
<input type = "password" id = "password" placeholder = "Enter Password">
</br></br></br></br>
<input type = "submit" onclick ="return validate()" value="Login">
</form>
</div>
</div>
</body>
<script type = "text/javascript">

function validate(){
if(document.getElementById("username").value=="Tushar"&&document.getElementById("password").value=="tushar"){
alert("Hello");
document.getElementById("content").innerHTML = '<h1 id = "welcome" style="font-size: 25px;color:red; ">Welcome</h1>';
return false;
}
else{
alert("Invalid username/password");
return false;
}
}
</script>

关于javascript - 为什么 innerHTML 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48250147/

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