Making jQuery's .prev().prev().prev().find("selector") calls easier
How can I avoid the .prev().pre开发者_开发百科v().prev()
calls? Is there a shortcut for this?
You can use .prevAll
, combined with eq
or :eq
, since prevAll
returns a set of element in the reverse order starting with the element that's closest to the current element being [0]
, the equivalent to .prev().prev().prev()
would be .prevAll().eq(2)
or .prevAll(':eq(2)')
.
See this simple demo: http://www.jsfiddle.net/QZYHN/
You can do prevAll()
with a selector.
$('div').prevAll('a:last')
Try closest
which searches for the nearest ancestor that matches the selector. eg.
$(this).closest('table').find('selector')
Edit: Added find() at the end
take a look at nth-child
in jquery, might be of help...
精彩评论