jquery: how to traverse upwards incrementally (adding parent()) via function?
this code below selects all the given children from the parent.
$(this).parent().find(this.attr("tagName").toLowerCase())
however, i dont know how to traverse upwards with the parent(). i would require a function that would automatically keep adding .parent() each time the function is called.
For example, i call function once and it return开发者_如何学运维s
$(this).parent().find(this.attr("tagName").toLowerCase())
I call the function for second time again and it returns
$(this).parent().parent().find(this.attr("tagName").toLowerCase())
Third time,
$(this).parent().parent().parent().find(this.attr("tagName").toLowerCase())
$('p:eq(0)').parents().each(function() {
var el = $(this).find( $(this).attr('tagName').toLowerCase() );
if ( el.length ) {
console.log(el);
}
});
JQuery has a function equivalent to the find
which works upwards rather than downwards called closest
added in jQuery 1.3
- http://api.jquery.com/closest/
For example
$(this).parent().closest(this.attr("tagName").toLowerCase())
精彩评论