I am trying to set a custom style for elements that are
- ul – the first in many children (ul.first)
- last in many children (ul.last)
- the only child (ul.first.last or ul.only)
HTML:
<ul class="selector">
<li class="selector">some li
<ul class="">
<li>some fancy li - the ul parent should have first class</li>
</ul>
<ul class="">
<li>some more li - the ul parent should have last class</li>
</ul>
</li>
<li class="selector">some second li
<ul class="">
<li>some lonely li - the ul parent should have first and last classes</li>
</ul>
</li>
</ul>
so I came up with a piece of jQuery hoping to do the trick
$('ul.selector li.selector > ul').each(function () {
if ($(this).is(':first') && $(this).is(':last')) {
$(this).addClass('first last');
} else if ($(this).is(':first')) {
$(this).addClass('first');
} else if ($(this).is(':last')) {
$(this).addClass('last');
}
});
Thanks for any suggestion.
UPDATE – here is a fiddle you can play with, based on one of the answers: http://jsfiddle.net/qAtpe/
Why not just this :
Then use the following CSS
Update
Looking at your example jsfiddle – you needed the selector
:first-childrather and:first..This now works :
Working example