How can I write this jQuery snippet more beautiful
$("#rightCol").children().children().children("div.entry").length
I tried this
开发者_开发问答$("#rightCol").children().eq(1).children("div.entry").length
or this
$("#rightCol").children(":eq(1)").children("div.entry").length
with no success. ideas?
Depending on the layout (do you have DIVs with that class at other levels that you want to avoid?), you might get by with
$('#rightCol').find('div.entry').length
You can do it using the child-selector (>
), like this:
$("#rightCol > * > * > div.entry").length
Though, if you know the child type, I'd use that over *
. If he level doesn't matter, just a descendant selector () will work.
$("#rightCol div.entry").length
Maybe this?
$("#rightCol *:eq(1) div.entry").length
This should work, if you must have the specific level in the descendant tree.
$("#rightCol > * > * > div.entry")
However, this is more normal. It would pick any "div.entry" inside of "#rightCol", regardless of the depth.
$("#rightCol div.entry")
精彩评论