我在 DOM 中有以下结构:
<div class="parent">
<div class="child0"></div>
<div class="child1"></div>
<div class="child2"></div>
<div class="child3"></div>
<div class="child4"></div>
<div class="child5"></div>
...
</div>
我尝试在 javascript 中做的是仅显示特定元素并隐藏其他元素,如下所示:
showItems(0,3,'.parent'); // show child 0,1,2
function showItems(offset,limit,component)
{
$(component).children().hide();
for(var i=offset;i<=(offset+limit);i++)
{
$(component+':nth-child('+i+')').show(); //!!! does not make them visible
}
}
有什么想法吗?
谢谢!
最佳答案
组件和 :nth-child 之间需要一个空格。因为现在您选择的是类为“parent”的任何元素,它也是第 n 个子元素。
关于javascript变量作为jquery中的选择器来获取特定的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281561/