Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么innerHTML不起作用?_Javascript_Html - Fatal编程技术网

Javascript 为什么innerHTML不起作用?

Javascript 为什么innerHTML不起作用?,javascript,html,Javascript,Html,我试图使用innerHTML更改id为内容的div元素,但它在登录时只会闪烁welcome然后消失。我知道这很愚蠢,但不明白为什么会出现此错误。 代码如下: <html> <head> <meta charset = "utf-8"> <link rel="stylesheet" href="signup.css"> </head> <body> <div id="content" styl

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

<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>


用户名:



密码:



函数验证(){ if(document.getElementById(“用户名”).value==“Tushar”和&document.getElementById(“密码”).value==“Tushar”){ 警惕(“你好”); document.getElementById(“内容”).innerHTML='Welcome'; } 否则{ 警报(“无效用户名/密码”); } }

但是欢迎不显示

Add return false;在函数的末尾

或者,正如人们所评论的,查看阻止表单提交的方法,或者更好的是,查看可以在事件对象上调用的方法(如果您提供变量/参数,这些对象将被传递给单击事件)。

Add return false;在函数的末尾


或如人们所评论的,注意防止表单提交,或者更好的是,查看可在事件对象上调用的方法(如果提供变量/参数,则传递到单击事件)。

< P>而不是Tyeth建议的,考虑使用< /P>
event.preventDefault();
以防止页面重新加载。像这样的,

function validate() {
    event.preventDefault();

    // Further code down.
这将阻止执行默认操作,并将显示您刚才添加的页面内容。因为,不是每个函数都必须
返回false,如果您使用linter或其他工具(如TypeScript),它也可能会破坏逻辑-它会假设您想要返回bool值,并会这样指示


代替Tyeth提出的建议,考虑使用

event.preventDefault();
以防止页面重新加载。像这样的,

function validate() {
    event.preventDefault();

    // Further code down.
这将阻止执行默认操作,并将显示您刚才添加的页面内容。因为,不是每个函数都必须
返回false,如果您使用linter或其他工具(如TypeScript),它也可能会破坏逻辑-它会假设您想要返回bool值,并会这样指示


请使用下面提到的代码..即更改颜色:#ffffff;由于您使用的是白色,所以无法看到其他颜色



用户名:



密码:



函数验证(){ if(document.getElementById(“用户名”).value==“Tushar”和&document.getElementById(“密码”).value==“Tushar”){ 警惕(“你好”); document.getElementById(“内容”).innerHTML='Welcome'; 返回false; } 否则{ 警报(“无效用户名/密码”); 返回false; } }
请使用下面提到的代码..即更改颜色:#ffffff;由于您使用的是白色,所以无法看到其他颜色



用户名:



密码:



函数验证(){ if(document.getElementById(“用户名”).value==“Tushar”和&document.getElementById(“密码”).value==“Tushar”){ 警惕(“你好”); document.getElementById(“内容”).innerHTML='Welcome'; 返回false; } 否则{ 警报(“无效用户名/密码”); 返回false; } }
您需要在代码中使用来阻止默认事件。或者

我不确定您将在何处使用此代码,但请小心使用密码/用户名

功能验证(e){
e、 预防默认值();
var$username=document.getElementById(“用户名”);
var$password=document.getElementById(“密码”);
var$content=document.getElementById(“内容”);
var isValidCredentials=($username.value==“Tushar”&&&$password.value===“Tushar”)
如果(!isValidCredentials){
$content.innerHTML='';
$content.style.display='none';
window.alert('Invalid');
回来
}
//window.alert('Welcome!');
$content.style.display='block';
$content.innerHTML='Welcome';
}
document.getElementById('form')。addEventListener('submit',validate,false)

用户名:




密码:



您需要在代码中使用来阻止默认事件。或者

我不确定您将在何处使用此代码,但请小心使用密码/用户名

功能验证(e){
e、 预防默认值();
var$username=document.getElementById(“用户名”);
var$password=document.getElementById(“密码”);
var$content=document.getElementById(“内容”);
var isValidCredentials=($username.value==“Tushar”&&&$password.value===“Tushar”)
如果(!isValidCredentials){
$content.innerHTML='';
$content.style.display='none';
window.alert('Invalid');
回来
}
//window.alert('Welcome!');
$content.style.display='block';
$content.innerHTML='Welcome';
}
document.getElementById('form')。addEventListener('submit',validate,false)

用户名:




密码:



试试replac