开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜