开发者

jQuery - How to select the first parent of an element and add a new class "testclass"

开发者 https://www.devze.com 2023-01-09 17:06 出处:网络
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\').

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号