Reputation: 644
I have a lit like this:
<ul id="categoria"> <br>
<li><a href="#">Categoria1</a></li>
<li><a href="#">Categoria2</a>
<ul class="sub-menu">
<li><a href="#">Esercizio1</a></li>
<li><a href="#">Esercizio2</a></li>
</ul>
</li>
<li><a href="#">Categoria3</a></li>
<li><a href="#">Categoria4</a>
<ul class="sub-menu">
<li><a href="#">Esercizio1</a></li>
<li><a href="#">Esercizio2</a></li>
</ul>
</li>
<li><a href="#">Categoria5</a></li>
</ul>
I want to set the background color, for example red , but just for the children of the list. So in this case Esercizio1, Esercizio2, ecc. with jquery. How is possible to do it? I only know to do for the whole list.
Upvotes: 0
Views: 1210
Reputation: 21501
You can use this selector.
$("#categoria li ul li > a").css({backgroundColor: "red" });
or
$(".sub-menu li > a").css({backgroundColor: "red" });
You can learn about jQuery selectors here.
This can also be achieved using CSS:
#categoria li ul li > a { background-color: red; }
or
.sub-menu li > a { background-color: red; }
Upvotes: 1
Reputation:
you can do that like this:
$('element').children().css({'background-color':'red'});
in your case:
$('ul li').children().css({'background-color':'red'});
a more detailed version:
$(".elementClassName li").children().css({'background-color':'red'});
you can specify for example only p tag children:
$(".elementClassName li").children('p').css({'background-color':'red'});
Upvotes: 1