在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 為什么內(nèi)部寫了阻塞代碼的script標(biāo)簽放到底部不會讓DOM先渲染出來呢?

為什么內(nèi)部寫了阻塞代碼的script標(biāo)簽放到底部不會讓DOM先渲染出來呢?

看JS紅寶書時遇到的困惑。 在解釋器對<script>元素內(nèi)部的所有代碼求值完畢以前,頁面中的其余內(nèi)容都不會被瀏覽器加載或顯示。 不理解這句話。

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Page Title</title>
</head>

<body>

    <h1>There are some html codes...</h1>
    <h2>There are some html codes...</h2>
    <h3>There are some html codes...</h3>

</body>

</html>
<script>
    function fun() {
        alert("There are some javascript codes")
    }
    alert("There are some other javascript codes")
    fun()
</script>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
</head>

<body>

    <h1>There are some html codes...</h1>
    <h2>There are some html codes...</h2>
    <h3>There are some html codes...</h3>

</body>
<script src="./js/1.js"></script>
<script src="./js/2.js"></script>

</html>

上面兩段代碼JS都是alert阻塞。第一次加載頁面 后面一段可以看到DOM內(nèi)容 前面一段不能。

如果說前面一段代碼還沒執(zhí)行完導(dǎo)致DOM沒有解析出來,那為什么后面一段代碼遇到script標(biāo)簽并且加載進(jìn)來解析JS文件發(fā)現(xiàn)是阻塞代碼的時候DOM先解析出來了呢?

回答
編輯回答
青檸

你script標(biāo)簽位置不一樣啊

2018年3月12日 22:37