我需要检查另一个类的块内是否存在一个类。块可以很多。嵌套可以是任意的。请告诉我我做错了什么。完整代码在这里https://codepen.io/BK-wmg12/pen/vdjxav 提前谢谢你:-)
<head>
<style>
.red {
background-color: red;
}
</style>
</head>
<body>
<body>
<div class="test">
<div class="my">
<span class="split1">
Some TEXT1
</span>
</div>
<div class="my">
<span >
Some TEXT2
</span>
</div>
<div class="my">
<span class="split1">
Some TEXT3
</span>
<script>
var els = document.getElementsByClassName('my');
for( var i = 0; i < els.length; i++ ) {
for( var j = 0; j < els[i].length; j++ ){
if( !els[i].childNodes[j].classList.contains('split1') ){
els[i].childNodes[j].classList.add('red');
}
}
}
</script>
</body>