I have this selector:
$('ul:not(.sub-menu) > li, ul.sub-menu > li:last-child').not(':has(ul.sub-menu)');
which gets these elements:
- Every direct
lidescendant whose parent is not.sub-menu(unless it’s the last, cf. 2.) - Every last li element of an
ul.sub-menu - Exclude all
li‘s that are parent to aul.sub-menu
Now, I want to use this in an if-statement, which is inside a hover-function. In other words, it’s got to be relative to that list item instead of being the list item.
Something like this:
$("nav li").hover(function () {
var $this = $(this);
if (($this.parent("ul:not(.sub-menu)") || ($this.parent("ul.sub-menu") && $this.is(":last-child"))) && $this.not(":has(ul.sub-menu)")) {
// do something
}
});
But this does not work. I think it has something to do with combining the OR and AND selectors inside an if-statement, but I can’t seem to debug it: the code above selects all list items without a distinction.
$this.parent("ul:not(.sub-menu)")is never falsy, even if the set is empty.Use
$this.parent("ul:not(.sub-menu)").lengthinstead in your condition.The whole if statement would thus look like this :
Details :
As written by the MDN :