2014-10-27 73 views
0

我想才达到这样的向上遍历(父),然后向下(孩子)

var argument = $(this).parent().parent().parent().prev().child().attr('ctryid'); 

这里就是我想穿越部分的剪断。 我想从newProvButtonctryid的元素。

它一团糟,对不起。

enter image description here

回答

3

您可以使用closest()找到与给定的选择相关的父元素。试试这个:

var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid'); 

你还应该注意在你的HTML中使用非spec属性会使你的页面无效。使用data-*属性来代替:

<div class="expandListHeaderRow" data-ctryid="your-guid">Foo</div> 
var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').data('ctryid'); 
+0

谢谢罗里。对于技巧,也不知道。 – morne 2014-10-27 11:17:24

+1

感谢您修复错字@TusharGupta – 2014-10-27 11:34:24

+0

@RoryMcCrossan欢迎先生:) – 2014-10-29 05:25:59

1

您可以缩小使用.closest()选择:

$(this).closest('.expandListContent').prev().find('div').attr('ctryid'); 

$(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid'); 
+0

感谢Milind,您的工作很好,但对于我从@Rory McCrossan得到的提示,我想我会回答他的答案。 – morne 2014-10-27 11:21:52

+1

@mornenel:你应该morne。 :) – 2014-10-27 11:23:17