开发者

simplify jquery parents and childrens

开发者 https://www.devze.com 2023-01-11 15:25 出处:网络
I have to get a far parent\'s child\'s child\'s attribute href... now i can get that using this long statement

I have to get a far parent's child's child's attribute href...

now i can get that using this long statement

var uid = $(this).parents(':eq(7)').children( ).find('.p_cell')
.children(':eq(0)').children(':eq(0)').attr('hr开发者_运维技巧ef');

please any string using '>' and ' : ' that can simplify the statement

thanks

Pradyut

India


Without knowing the HTML, you can use :first-child like this:

var uid = $(this).parents(':eq(7)')
                 .find('> * .p_cell > :first-child > :first-child').attr('href');

If that parent has a class you can access though, instead of say the 8th parent you could find it via a selector that matched using .closest(). For example if that parent was a <div> like this:

<div class="container">

You could do this:

var uid = $(this).closest('.container')
                 .find('> .p_cell > :first-child > :first-child').attr('href');

However, if you posted the HTML this can likely get much simpler.

0

精彩评论

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