Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.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_Css - Fatal编程技术网

jquery所有第一个子项,无论深度如何

jquery所有第一个子项,无论深度如何,jquery,css,Jquery,Css,如何使用jquery只获取具有特定css类的所有第一个子项,而不管这些Html元素在DOM树中的深度如何?文档中有: 说明:选择作为其父元素的第一个子元素的所有元素 演示: $('div:first child').css({border:'1px solid'}) 我是第一个孩子 我是第二个孩子 我是第一个孩子 我是第二个孩子 身体的另一个div子元素就在文档中: 说明:选择作为其父元素的第一个子元素的所有元素 演示: $('div:first child').css({border:'

如何使用jquery只获取具有特定css类的所有第一个子项,而不管这些Html元素在DOM树中的深度如何?

文档中有:

说明:选择作为其父元素的第一个子元素的所有元素

演示:

$('div:first child').css({border:'1px solid'})

我是第一个孩子
我是第二个孩子
我是第一个孩子
我是第二个孩子

身体的另一个div子元素
就在文档中:

说明:选择作为其父元素的第一个子元素的所有元素

演示:

$('div:first child').css({border:'1px solid'})

我是第一个孩子
我是第二个孩子
我是第一个孩子
我是第二个孩子

主体的另一个div子级
如果您使用jQuery,您只需将类名作为目标,它将为您将所有实例抛出到一个数组中,而不管其深度如何

例如:

var children = $('.childclass');
console.log(children.length)//returns "3".
$(children[1]) //returns the second "childclass" instance
$(children[1]).css('color', 'red'); //will give the 2nd instance of the childclass red text.



否则,如果您希望在不使用jQuery的情况下单独将它们作为目标,则可以使用:first child选择器。

如果您使用jQuery,则只需将类名作为目标,它将为您将所有实例抛出到一个数组中,而不管其深度如何

例如:

var children = $('.childclass');
console.log(children.length)//returns "3".
$(children[1]) //returns the second "childclass" instance
$(children[1]).css('color', 'red'); //will give the 2nd instance of the childclass red text.



否则,如果希望在不使用jQuery的情况下单独将它们作为目标,可以使用:first-child选择器。

children[1]
返回dom元素…而不是jQuery对象。无法在dom元素上使用
css()
。另外,
length
是一个属性而不是一个函数。您是对的-我忘记将返回值包装在jQuery对象中。更新了我的答案,为什么?可以改用
eq()
。无论哪种方式,这都不符合OP的要求。
children[1]
返回的是dom元素,而不是jQuery对象。无法在dom元素上使用
css()
。另外,
length
是一个属性而不是一个函数。您是对的-我忘记将返回值包装在jQuery对象中。更新了我的答案,为什么?可以改用
eq()
。不管怎样,这都不符合OP的要求