Select List Item Only If Doesn’t Contain Another List (and is top level)

Avatar of Chris Coyier
Chris Coyier on

I realize this is rather specific, but I had to write this selector earlier to fix a problem and I used jQuery because the selector is rather advanced (and needed to work cross-browser). I needed to select the anchor link of a list item but only if that list item didn’t contain another list and was at the top level of the nested list structure (no deeper).

$("ul.dropdown > li:not(:has('ul')) a").css({
        "background-image": "none",
});

The idea was that each of the top level links in the dropdown menu had a “down arrow” graphic, but the list items that did not have a dropdown should have that arrow removed.