The tag generated by JS is different from the CSS with the same native tag.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
-sharpul{ width:50%;}
li{ background:-sharpf5f5f5; border-bottom:1px solid -sharpddd; padding:5px 0; }
-sharpul li:nth-child(n+5){ background:-sharpF90}
</style>
</head>
<body>
<ul id="ul">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li>11</li>
    <li>12</li>
</ul>
</body>
</html>

the above code selects

from the fourth child element.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
-sharpul{ width:50%;}
li{ background:-sharpf5f5f5; border-bottom:1px solid -sharpddd; padding:5px 0; }
-sharpul li:nth-child(n+5){ background:-sharpF90}
</style>
</head>
<body>
<ul id="ul">
<script>
for(var i=1;i<=12;iPP){
    document.write("<li>" + i + "</li>")
}
</script>
</ul>
</body>
</html>

the above code selects

from the third child element.

Why is this?

Mar.04,2021

@ kid web @ DriftKing both of you are right!
there are two solutions
first: if HTML and JS remain the same, you can replace the CSS selector nth-child with CSS of JS!

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-CSS3</title>
<style>
-sharpul{ width:50%;}
li{ background:-sharpf5f5f5; border-bottom:1px solid -sharpddd; padding:5px 0; }
-sharpul li:nth-of-type(n+5){ background:-sharpF90}
/*ul li:nth-child(-n+2){ background:-sharpccc}*/
</style>
</head>
<body>
<ul id='ul'>
<script>
for(var i=1;i<=12;iPP){
    document.write('<li>' + i + '</li>')
}
</script>
<!--
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li>11</li>
    <li>12</li>
-->        
</ul>
</body>
</html>

the second solution, CSS unchanged, is to write JS outside the parent element as DriftKing says, and add in:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-CSS3</title>
<style>
-sharpul{ width:50%;}
li{ background:-sharpf5f5f5; border-bottom:1px solid -sharpddd; padding:5px 0; }
-sharpul li:nth-child(n+5){ background:-sharpF90}
/*ul li:nth-child(-n+2){ background:-sharpccc}*/
</style>
</head>
<body>
<ul id='ul'>
<!--
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li>11</li>
    <li>12</li>
-->        
</ul>
<script>
var u = document.getElementById("ul"); 
for(var i=1;i<=12;iPP){
    var li = document.createElement("li"); 
    li.innerText = i;
    u.appendChild(li); 
}
</script>
</body>
</html>


with the tag generated by js, you can see the structure in the console as follows:
clipboard.png
ul<script></script>

<script></script>
clipboard.png

feels like treating the scritp tab as a child. of ul


there is something wrong with the way you write this js. After the page is loaded, go directly to append and write it in html. The selector must regard it as a child tag

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eae475-21c0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eae475-21c0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?