开发者

jQuery Parent Class Attribute

开发者 https://www.devze.com 2023-01-24 02:17 出处:网络
I want to add a slide up affect with jQuery to a DIV when a link inside of th开发者_JAVA技巧e DIV is clicked, but the problem i am running into is the class of the DIV\'s are defined by a loop. So, i

I want to add a slide up affect with jQuery to a DIV when a link inside of th开发者_JAVA技巧e DIV is clicked, but the problem i am running into is the class of the DIV's are defined by a loop. So, i can't define the DIV class in the jQuery line, because each DIV class is different and i cannot determine ahead of time what they are. I am trying to use .parent and .child but I am not sure how to go about this. Is this making any sense?


Bind to the click of the element you want (in this case I just used a simple anchor element). Then find the first parent that is a div and perform the slideUp() effect.

$('a').click(function() {
    $(this).parents('div:first').slideUp();
});

You can see it work here: http://jsfiddle.net/XNnSp/


Let me know if that's what you are looking for http://jsbin.com/ehoza3

$('a').click(function() {
    $(this).parent().slideUp();
});


Two (most obvious) ways

FIRST
If your tree is always defined in terms of depth you could access your parent DIV doing just that:

$(this).parent().parent().parent().slideUp();

SECOND
Add an additional class that doesn't clash with dynamic ones and do this:

$(this).closest(".some-custom-class").slideUp();
0

精彩评论

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