Warum funktioniert nth-of-type/nth-child nicht für verschachtelte Elemente?
P粉203792468
P粉203792468 2023-10-23 10:39:24
0
1
435

Ich versuche, den Stil ungerader Divs innerhalb eines Divs zu ändern. Wenn sichnth-of-type(odd)in einem anderen Div befindet, wirkt sich das aus irgendeinem Grund auf alle meine Divs aus. Hier ist mein Code für reguläre Divs und ungerade Divs:


.video-entry-summary { width: 214px; height: 210px; margin-left: 10px; float: left; position: relative; overflow: hidden; border: 1px solid black; } .video-entry-summary:nth-of-type(odd) { width: 214px; height: 210px; margin-left: 0px; float: left; position: relative; overflow: hidden; border: 1px solid black; background: #ccc; }
video 1
video 2
video 3
video 4


Aus irgendeinem Grund funktioniertnth-of-typenicht, wenn es in meine Divs eingepackt ist, aber es funktioniert, wenn sie nicht in irgendwelche Divs eingewickelt sind.

Arbeitsversion, wenn nicht in ein Div eingeschlossen:


.video-entry-summary { width: 214px; height: 210px; margin-left: 10px; float: left; position: relative; overflow: hidden; border: 1px solid black; } .video-entry-summary:nth-of-type(odd) { width: 214px; height: 210px; margin-left: 0px; float: left; position: relative; overflow: hidden; border: 1px solid black; background: #ccc; }
video 1
video 2
video 3
video 4


Wie kann ich dafür sorgen, dass der ursprüngliche Code wie oben funktioniert?

P粉203792468
P粉203792468

Antworte allen (1)
P粉078945182

:nth-of-type():nth-child()类似,因为它们必须都来自同一个父级。如果您需要这些包装器div,请在这些包装器上使用:nth-of-type()

div.post:nth-of-type(odd) .video-entry-summary { width:214px; height:210px; margin-left:0px; float:left; position:relative; overflow:hidden; border:1px solid black; background:#ccc; }

如果所有同级都是.post,请使用:nth-child()以避免与:nth-of-type()的真正含义

.post:nth-child(odd) .video-entry-summary { width:214px; height:210px; margin-left:0px; float:left; position:relative; overflow:hidden; border:1px solid black; background:#ccc; }

.video-entry-summary { width: 214px; height: 210px; margin-left: 10px; float: left; position: relative; overflow: hidden; border: 1px solid black; } .post:nth-child(odd) .video-entry-summary { width: 214px; height: 210px; margin-left: 0px; float: left; position: relative; overflow: hidden; border: 1px solid black; background: #ccc; }

              
video 1
video 2
video 3
video 4
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!