JavaScript__之報錯處理

報錯提示文字

Uncaught TypeError: Cannot read property 'getElementsByTagName' of nulljavascript

報錯控制檯顯示圖片

html代碼顯示

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="../css/index.css">
    <!--這一種是在底部加載類容-->
    <script src="../js/index.js"></script>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="main" id="main">
    <!--圖片輪播-->
    <div class="banner" id="banner">
        <a href="">
            <div class="banner-slide slide1 slide-active"></div>
        </a>
        <a href="">
            <div class="banner-slide slide2"></div>
        </a>
        <a href="">
            <div class="banner-slide slide3"></div>
        </a>
    </div>
    <!--上一張,下一章圖片-->
    <a href="javascript:void(0);" class="button pre"></a>
    <a href="javascript:void(0);" class="button next"></a>
    <!--設置小圓點-->
    <div class="dots">
        <ul>
            <span class="active"></span>
            <span></span>
            <span></span>
        </ul>
    </div>
</div>
</body>
</html>
複製代碼

解決過程,發現javaScript不該該寫在head中,應該寫在body中

更改後的代碼css

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="../css/index.css">

    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="main" id="main">
    <!--圖片輪播-->
    <div class="banner" id="banner">
        <a href="">
            <div class="banner-slide slide1 slide-active"></div>
        </a>
        <a href="">
            <div class="banner-slide slide2"></div>
        </a>
        <a href="">
            <div class="banner-slide slide3"></div>
        </a>
    </div>
    <!--上一張,下一章圖片-->
    <a href="javascript:void(0);" class="button pre"></a>
    <a href="javascript:void(0);" class="button next"></a>
    <!--設置小圓點-->
    <div class="dots">
        <ul>
            <span class="active"></span>
            <span></span>
            <span></span>
        </ul>
    </div>
</div>
<!--這一種是在底部加載類容-->
<script src="../js/index.js"></script>
</body>
</html>
複製代碼

最後運行結果

網頁是從頭部向下加載的,假如加載到頭部的時候,這個時候尚未加載到div的時候就會報錯
相關文章
相關標籤/搜索