Javascript 无法读取null的属性“style”

Javascript 无法读取null的属性“style”,javascript,html,Javascript,Html,关于这个完全相同的错误,这里还有很多其他的问题,但是我没有找到任何解决办法 以下是HTML: <!DOCTYPE html> <html> <head> <title>ChatRoom</title> <link rel="stylesheet" href="css/style.css"> <link href='https://fonts.googleapis.com/css?family=R

关于这个完全相同的错误,这里还有很多其他的问题,但是我没有找到任何解决办法

以下是HTML:

<!DOCTYPE html>
<html>
<head>
    <title>ChatRoom</title>
    <link rel="stylesheet" href="css/style.css">
    <link href='https://fonts.googleapis.com/css?family=Raleway:400,200' rel='stylesheet' type='text/css'>
    <script src="js/main.js"></script>
</head>
<body>
    <div id="container">
        <div id="logo">
            <a href="./">
                <h1>ChatRoom</h1>
                <h3>Basic Chatroom<h3>
            </a>
        </div>
        <div id="joinForm">
            <div class="tHeader">
                <h2>Join A ChatRoom</h2>
            </div>
            <div class="formArea">
                <form action="joinRoom.php" method="GET">
                    <h3>Name Of Room:</h3>
                    <input type="text" name="roomName" class="jmInput" placeholder="Name Of Room">
                    <h3 class="margin-top">Password:</h3>
                    <input type="text" name="roomName" class="jmInput" placeholder="Password">
                    <input type="submit" name="roomName" class="jmInput" value="Join ChatRoom">
                </form>
            </div>
        </div>
    </div>
</body>
</html>
显然,document.getElementById'joinForm'是空的。我真的不知道该怎么解决这个问题

任何帮助都将不胜感激。提前感谢:

window.onload=fadeIn;调用fadeIn并将其返回值分配给window.onload,就像x=foo调用foo并将其返回值分配给x一样。因此,您的fadeIn没有等待加载事件。因此,假设您正在使用getElementById查找的元素还不存在,因此getElementById返回null

删除以分配函数引用:

window.onload = fadeIn;
旁注:加载事件在页面加载过程中发生得很晚,等待加载所有图像和其他外部资源。通常,您只想将脚本标记放在页面的最底部,就在结束标记之前,并立即运行您的函数-它将可以访问所有元素,因为它们是在它上面定义的。但是,如果您有等待加载的理由,只需删除

window.onload = fadeIn;