使用vs code 的open with live server遇到的問題(偽類選擇器)


剛剛寫了段代碼發現了個不太注意的問題

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
        }
        div:last-child{
            background-color: red;
            left:50px;
            top:50px;
            /*z-index: 1000;*/
        }
        div:first-child{
            z-index: 100;
        }
    </style>
</head>
<body>
    <div>div1</div>
    <div>div2</div>
</body>
</html>

當點擊open with live server(實時服務器打開時效果是第一張圖,其他瀏覽器打開是這樣)

 

 

 

 (圖1)

 

 

 (圖2)

原因在於

div:last-child{ background-color: red; left:50px; top:50px; /*z-index: 1000;*/ }
選中的是div作為父元素的最后一個孩子,而實時服務器會加上一句
<script type="text/javascript">導致最后一個孩子是<script>,從而導致出來的樣式和理想中的不同。

 

 
        

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM