JSFiddle - React, Tailwind, and code Playground

HTML

<div class="parent">1
  <div>2
    <div class="colorParents">3</div>
  </div>
</div>

CSS

div{
    height:50px;
    width:50px;
    display:block;
}

div.parent{
    background-color:red;
}

JavaScript

$.each($('.colorParents').parentsUntil('.parent').addBack(),function(i){
    if(i%2){
        $(this).css('background-image','http://www.inetshops.at/gourmesso-logo.jpg');
    }else{
        $(this).css('background-color','blue');
    }

});