2014-01-16 56 views
0

下面是我循环交换两个div的内容的循环,如何获得子循环jQuery中的父循环元素?

$('#itemsRows ul').each(function() { 
    var $this = $(this); 



    $('#wrap-ajax ul').each(function() { 
     // How to get #itemsRows ul input element? 

    }); 

    }); 
+2

'$ this'第二回调指的是'#itemsRows ul'元素中 –

回答

2

通过对$this使用findchildren

$('#itemsRows ul').each(function() { 
    var $this = $(this); 



    $('#wrap-ajax ul').each(function() { 
     var theInput = $this.find("input"); 
     // Or 
     var theInput = $this.children("input"); 
    }); 

}); 

...这取决于你是否希望子孙元素(如您的问题建议; find),或只是直接的孩子(children)。

你的内循环的迭代函数关闭了在它的创建,包括你的$this变量的上下文,所以即使你内心的迭代函数内部,$this指为外环的具体#itemsRows ul元素。

更多有关闭(在我的博客):Closures are not complicated