thanks for your time and first of all I'm a noobie who's been learning by his own javascript so I don't manage quite good the selectors to achieve what I want. Here it's my problem:
html
<ul id='stuff' class='goods'>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li><span class='highlighted'>list item 4</span></li>
<li>list item 5</li>
</ul>
<ul id='stuff2' class='goods'>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>item 4</li>
<li>list item 5</li>
</ul>
CSS
.highlighted{
color: red;
}
.goods{
border:1px solid red;
cursor: pointer;
padding: 0px;
list-style: none;
}
.match_group{
border: 1px solid black;
}
Look here pelase: * http://jsfiddle.net/nero_noob_123/ctcn7kfy/2/
basically what I want to achieve with the jquery and javascript it's the following.... but I don't know how write it as a condition so try to read it as follows:
$('#stuff2').addClass('match_group');
$('#stuff2').addClass('match_group');
if($('li').has('span.highlighted').length){ //so span exist!
$('span.highlighted').closest('.goods').addClass('match_group');
}else if($('.goods').hasClass('match_group') && // span.highlight DOESN'T EXIST inside .goods) {
/// remove the match_group class from '.goods' where span.highlight doesn't exist
}else{ if any of the '.goods' classes has span.highlighted inside them, don not apply .match_group
}
I hope I have made myself clear, and sorry that's the best I can do with my current knowledge in JQ and javascript
PD: feel free to modify the crapy code I wrote to acheive what I need, thank you!
You can pass a second parameter to toggleClass
that will turn on or off the specified class. https://api.jquery.com/toggleClass/
That can be a boolean value, or (and this does not appear to be documented) it can be a function returning a boolean result!
$('.goods').toggleClass('match_group', function() {return $(this).has("span.highlighted");});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.