jQuery - How to select the first parent of an element and add a new class "testclass"
First Name
Given element 'element_1', how to select its first parent with tag name as li?
Tha开发者_Go百科nk you
$('#element_1').closest('li').addClass('testclass');
$('#element_1').closest('li').addClass('testclass');
assuming you're using jQuery 1.3+
and element_1
is an ID
.
jQuery
< 1.3 should look like
$('#element_1').parents('li').addClass('testclass');
Reference: .closest(), .parents()
精彩评论