Please help me out with the following code. I don’t understand it. I have to use a snippet like this in my project.
$('strong', this) <- this part is not clear to me at all.
Please be kind enough to explain the whole code line by line if possible.
<ul>
<li><strong>list</strong> item 1 -
one strong tag
</li>
<li><strong>list</strong> item <strong>2</strong> -
two <span>strong tags</span>
</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
JavaScript:
$('li').filter(function(index) {
return $('strong', this).length == 1;
}).css('background-color', 'red');
$('strong', this)is jQuery selector with$(target, context)format.According to your code:
thisrefers toliand$('strong', li)is searching a<strong>that within thatlitag.This statement can also be written as:
$(this).find('strong')and from jQuery library code you’ll see that:$(target, context)format internally implement the$(context).find(target)process.For more see here.