實踐出真知:JS執行順序

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html {
            margin: 0;
            padding: 0;
        }

        body {
            margin: 0;
            padding: 0;
        }
    </style>
    <script>
        console.log('head before Mounted:' + document.querySelector('#box')?.innerHTML);
        
        window.addEventListener('DOMContentLoaded', function (e) {
            console.log('head dom content loaded: ' + document.querySelector('#box').innerHTML);
        });

        window.onload = function (e) {
            console.log('head window onload: ' + document.querySelector('#box').innerHTML);
        }
    </script>
</head>

<body>
    <script>
        console.log('before Mounted:' + document.querySelector('#box')?.innerHTML);
    </script>
    <div id="box">box</div>[圖片上傳中...(image.png-1bc259-1676256319808-0)]

    <script>
        console.log('dom end: ' + document.querySelector('#box').innerHTML);
    </script>

    <script>
        window.onload = function (e) {
            console.log('window onload: ' + document.querySelector('#box').innerHTML);
        }
    </script>

    <script>
        window.addEventListener('DOMContentLoaded', function (e) {
            console.log('dom content loaded: ' + document.querySelector('#box').innerHTML);
        });      
    </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章