Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 选择一个集合并在范围(或功能)内使用该集合_Jquery - Fatal编程技术网

Jquery 选择一个集合并在范围(或功能)内使用该集合

Jquery 选择一个集合并在范围(或功能)内使用该集合,jquery,Jquery,我不太清楚如何恰当地解释这一点。 我想要的是选择一个元素,它就像孩子一样 $('div#foo'); 然后处理这个集合,这样jQuery只知道这个集合,所以它是这样的: $('div#foo').function() { $('div#firstChildOfFoo').attr('width'); // DO OTHER STUFF WITH THIS SET. } 如何实现这一点?您可以使用 更新 你在找这样的东西吗?这里我演示如何使用选择器查找集合$foo范围内的元素

我不太清楚如何恰当地解释这一点。 我想要的是选择一个元素,它就像孩子一样

$('div#foo');
然后处理这个集合,这样jQuery只知道这个集合,所以它是这样的:

$('div#foo').function() {
    $('div#firstChildOfFoo').attr('width');
    // DO OTHER STUFF WITH THIS SET.
}
如何实现这一点?

您可以使用

更新

你在找这样的东西吗?这里我演示如何使用选择器查找集合
$foo
范围内的元素

bar($('div#foo'));

function bar($foo) 
{
    $(".some-selector", $foo).css('width', '10px');
    $(".some-other-selector", $foo).css('height', '50px');
    // DO OTHER STUFF WITH THIS SET.
}

$('#foo img').each(function(){

  alert($(this).width());

});

你只想要第一个孩子吗?或者你想要所有的子元素?我需要所有的子元素,整个集合。我知道,但我需要做的不仅仅是这一条语句,还需要获取集合中的其他元素。正因为如此,我想以某种方式将其包装到一个函数中。您对集合中的每个项都执行相同的操作吗?但这只会在集合中循环,对吗?我想模拟,所选集是整个DOM。而不仅仅是使用
*
选择器。如果我很了解你。。。所有父元素都应该有一个
.class
,并且应该有:
$('.foo img')。每个(function(){/*code HERE*/})而不是
#foo
$('#foo img').each(function(){

  alert($(this).width());

});