Remember that . has () returns jQuery , not boolean!
.has (selector) Returns: jQuery
Description. Reduce the set of matched elements to those who have a child that matches the selector element or DOM element.
Doing if(!$(this).has('p'))
always true, since jQuery always returns a set, even if it is empty!
.has()
basically filters the set on which it is called, so you just need to do:
$('.test').has('p').addClass('red');
source share