首页 > 解决方案 > 带有对象变量的 JQuery concat 选择器

问题描述

我在div中几乎没有.children元素。.parent我想循环子元素。

//This is working good.
$('.parent .children').each(function(){
     //working good.
});

//But I would like to do that in the following way-
var parent = $('.parent');
$(parent + ' .children').each(function(){

});

这次我收到以下错误-

jquery.min.js:2 未捕获错误:语法错误,无法识别的表达式:[object Object] .children

有什么帮助吗?

标签: jqueryselector

解决方案


您可以使用find()如下方法:

parent.find('.children').each(function() { 
  // Loop here
});

或者,parent作为上下文传递给 jQuery 构造函数,如下所示:

$('.children', parent).each(function() { 
  // Loop here
});

推荐阅读