开发者

jQuery.closest(); traversing down the DOM not up

开发者 https://www.devze.com 2023-01-03 13:22 出处:网络
Afternoon peoples. I am having a bit of a nightmare traversing a DOM tree properly. I have the following markup

Afternoon peoples.

I am having a bit of a nightmare traversing a DOM tree properly. I have the following markup

http://pastie.org/999519

... And I am trying to traverse from input.save at the bottom right the way up to div.node... This all works well with one copy of the markup but if I duplicate it (obvisouly changing the ID of the uppermost div.node and use jQuery.closest('div.node') for the upper of the div.node's it will return the element below it not the element a开发者_Python百科bove it (which is the right one). I've tried using parents() but that also has it's caveats. Is there some kind of contexyt that can be attached to closest to make it go up and not down? or is there a better way to do this.

Thanks in advance.

Alex

P.S It seems that stackoverflow has done something weird to the markup! - I just triple checked it and it is fine but for some reason it's concate'd it below


First of all, you really should use an id for the .save element, unless you have more, and if so then use a node prefix if you can, so $('input.save')...

I'm not sure what the problem is with your code. You say it is traversing down when you want it to go up? It is supposed to go up.

From jQuery docs at http://api.jquery.com/closest/:

Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree

Maybe there is some problem with using .find() right after closest(). However, I don't think that would be a problem.

0

精彩评论

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