Question

I have a selector, "td > a.leftmenuitem:last, div > a.leftmenuitem:last", and I'd like to simplify it a little. I've tried "* > a.leftmenuitem:last", "td, div > a.leftmenuitem:last", and "(td, div) > a.leftmenuitem:last", none of which work the way the first selector does. Is this kind of thing just not possible in the selector syntax without making a separate selector for each?

Was it helpful?

Solution

You can't simplify:

td > a.leftmenuitem:last, div > a.leftmenuitem:last

without there being something in common so you can select all relevant td and div elements with one expression. For example if they both had class blah you could do:

.blah > a.leftmenuitem:last

But do not use this kind of expression if you can avoid it. Class selectors are slow (compared to ID and tag selectors).

Is there something wrong with just?

a.leftmenuitem:last

?

By the way, are you certain :last is the correct choice? A lot of people mistakenly using it, mistaking it for :last-child. :last will only match one element, the last matching element found on the page.

OTHER TIPS

What is bigger, sometimes is faster. When you use * instead of td, your query is much slower.

I don't think you can simplify it. I'd instead opt towards giving the links you're interested in a unique class, so that you can simply do "a.<my-unique-class>:last". That way jquery can simply scan all matching <a> tags without worrying about their parent being a <td> element.

Your selector is simple enough. If you wanted to simplify it, you could do something like:

$('a.leftmenuitem:last').parent().doWhateverYouWereGoingToDo();

I doubt this would result in faster execution times, however.

Maybe I'm just confused, but if

* > a.leftmenuitem:last

would work for you, then why not go with

a.leftmenuitem:last

instead?

The selector you're using is

Parent > Child 

match all child elements specified by Child of elements specified by Parent. Unless you are using <a> elements elsewhere in the page that are not children of <div> and <td> elements, why not just use

$('a.leftmenuitem:last')

Am I missing something?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top