我有三个具有相同类别的无序列表。我遍历它们,并尝试向它们每个添加与某些描述匹配的项目,但是当我尝试通过其索引引用列表时,它说找不到附加函数。代码看起来像这样:
demoTypes.upcomingDemos.map(function(item) {
var itemElement = "<li>My Item</li>";
if (demoTypes.type == "Basic") {
$(".unorderedListSection")[0].append(itemElement);
} else if (demoTypes.type == "Intermediate") {
$(".unorderedListSection")[1].append(itemElement);
} else if (demoTypes.type == "Advanced") {
$(".unorderedListSection")[2].append(itemElement);
}
});
由于某些原因,将项目添加到所有列表中似乎可以正常工作(尽管我显然不想这样做):
$(".unorderedListSection").append(itemElement);
当通过索引访问jQuery对象时,它返回一个DOMElement而不是jQuery对象,因此您会收到有关缺少append()
方法的错误。
要解决此问题,请使用以下eq()
方法:
demoTypes.upcomingDemos.map(function(item) {
var itemElement = "<li>My Item</li>";
if (demoTypes.type == "Basic") {
$(".unorderedListSection").eq(0).append(itemElement);
} else if (demoTypes.type == "Intermediate") {
$(".unorderedListSection").eq(1).append(itemElement);
} else if (demoTypes.type == "Advanced") {
$(".unorderedListSection").eq(2).append(itemElement);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句