<div class="second">
<div class="selector" id="selFirst"></div>
<div class="selector" id="selSecond"></div>
<div></div>
</div>
How to get #selFirst using element index not the ID?
如何使用元素索引来获取#selFirst,而不是ID?
this:
这样的:
var $selFirst = $(".second:nth-child(1)");
console.log($selFirst);
is returning :
返回:
jQuery(div.second)
6 个解决方案
#1
152
If you know the child element you're interested in is the first:
如果你知道你感兴趣的子元素是第一个:
$('.second').children().first();
Or to find by index:
或按指数查找:
$('.second').children().eq(0);
#2
11
There are the following way to select first child
有以下方法选择第一个孩子。
1) $('.second div:first-child')
2) $('.second *:first-child')
3) $('div:first-child', '.second')
4) $('*:first-child', '.second')
5) $('.second div:nth-child(1)')
6) $('.second').children().first()
7) $('.second').children().eq(0)
#3
3
You can get first element via index selector:
你可以通过索引选择器获得第一个元素:
$('div.second div:eq(0)')
Code: http://jsfiddle.net/RX46D/
代码:http://jsfiddle.net/RX46D/
#4
2
$('.second').find('div:first')
#5
1
Doesn't nth-child return siblings rather than children?
不是孩子,而是兄弟姐妹吗?
var $selFirst = $(".second:nth-child(1)");
will return the first element with the class '.second'.
将返回第一个元素与类'.second'。
var $selFirst = $(".selector:nth-child(1)");
should give you the first sibling of class '.selector'
应该给你类的第一个兄弟类。选择器
#6
-7
var node = document.getElementsByClassName("second")[0].firstElementChild
var = document.getElementsByClassName节点(“第二”)[0].firstElementChild
Disclaimer: Browser compliance on getElementsByClassName
and firstElementChild
are shaky. DOM-shims fix those problems though.
免责声明:浏览器对getElementsByClassName和firstElementChild的遵从性是不可靠的。不过,domshims解决了这些问题。
#1
152
If you know the child element you're interested in is the first:
如果你知道你感兴趣的子元素是第一个:
$('.second').children().first();
Or to find by index:
或按指数查找:
$('.second').children().eq(0);
#2
11
There are the following way to select first child
有以下方法选择第一个孩子。
1) $('.second div:first-child')
2) $('.second *:first-child')
3) $('div:first-child', '.second')
4) $('*:first-child', '.second')
5) $('.second div:nth-child(1)')
6) $('.second').children().first()
7) $('.second').children().eq(0)
#3
3
You can get first element via index selector:
你可以通过索引选择器获得第一个元素:
$('div.second div:eq(0)')
Code: http://jsfiddle.net/RX46D/
代码:http://jsfiddle.net/RX46D/
#4
2
$('.second').find('div:first')
#5
1
Doesn't nth-child return siblings rather than children?
不是孩子,而是兄弟姐妹吗?
var $selFirst = $(".second:nth-child(1)");
will return the first element with the class '.second'.
将返回第一个元素与类'.second'。
var $selFirst = $(".selector:nth-child(1)");
should give you the first sibling of class '.selector'
应该给你类的第一个兄弟类。选择器
#6
-7
var node = document.getElementsByClassName("second")[0].firstElementChild
var = document.getElementsByClassName节点(“第二”)[0].firstElementChild
Disclaimer: Browser compliance on getElementsByClassName
and firstElementChild
are shaky. DOM-shims fix those problems though.
免责声明:浏览器对getElementsByClassName和firstElementChild的遵从性是不可靠的。不过,domshims解决了这些问题。