I use, a div container with different bg class like:
<!--Recipes-->
<div class="PozVideos BgBlue RadiusTopTen">
<div class="Caption">Yemek tarifleri</div>
<ul>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
</ul>
</div>
<!--Recipes-->
<!--Videos-->
<div class="PozVideos BgPurple RadiusTopTen">
<div class="Caption">Video Galeri</div>
<ul>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
</ul>
</div>
<!--Videos-->
<!--Gallery-->
<div class="PozVideos BgYellow RadiusTopTen">
<div class="Caption">Fotoğraf Galerileri</div>
<ul>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
<li><a href=""><img src="" alt=""></a></li>
</ul>
</div>
<!--Gallery-->
How can I add another different background class for each <li>
, for example;
if PozVideos class has BgYellow class, I want to add BgYellowForLi class each li element in this div.containner(PozVideos).
if PozVideos class has BgPruple class, I want to add BgPurpleForLi class each li element in this div.containner(PozVideos).
Is that possible with jQuery?
-
1You don't need to update your questions and tag them solved. Stack Overflow automatically distinguishes questions that have an accepted answer from questions that don't, with different styling.David Hedlund– David Hedlund2012年11月16日 10:44:10 +00:00Commented Nov 16, 2012 at 10:44
-
Allright. i ll be carrefful another coming up question :) Thans @David.Fatih Toprak– Fatih Toprak2012年11月16日 10:45:26 +00:00Commented Nov 16, 2012 at 10:45
5 Answers 5
$('.PozVideos.BgYellow li').addClass('BgYellowForLi');
But you may be better off changing your CSS:
.PozVideos li {
/* styles that apply for all li's in any PozVideos */
}
.PozVideos.BgYellow li {
/* styles that apply only to yellow li's */
}
1 Comment
$('.PozVideos.BgYellow').find('li').addClass('BgYellowForLi');
$('.PozVideos.BgPruple').find('li').addClass('BgPurpleForLi');
You can even write the code more like they way you describe what you want to do with some if statements
var $yellowElem = $('.PozVideos.BgYellow li');
if($yellowElem.length > 0) {
$yellowElem.addClass('BgYellowForLi');
}
3 Comments
Yes, that's possible, and not complicated:
"if PozVideos class has BgYellow class, i want to add BgYellowForLi class each li element in this div.containner(PozVideos)":
$('.PozVideos.BgYellow li').addClass('BgYellowForLi');
"if PozVideos class has BgPruple class, i want to add BgPurpleForLi class each li element in this div.containner(PozVideos)":
$('.PozVideos.BgPurple li').addClass('BgPurpleForLi');
Comments
Dynamic solution:
$('#PozVideos').each(function(){
var classList = $(this).prop('class').split(/\s+/);
$(this).find('li').addClass(classList[1] + 'ForLi');
});
1 Comment
PozVideos
isn't an ID, but I assume that was just a typo in your selector. Other than that, it feels hacky to make assumptions on the order of classes within the string. If I add a new class to an element, I generally don't expect to break a script. If it really needs to be dynamic, to cater for a big number of colors, say, I'd consider setting a data-color="BgYellow"
or something.This can be achieved like this too:
var bgParent = $('.BgYellow');
$('li',bgParent).css({"background":"blue"});