jQuery $.each() 多维 JSON 数组
我在这里做错了什么?我看不到它,通过第一个 $.each 它起作用,到达第二个它停止..
var testJSON = {"cluster":[{"node":[{"name":"one", "number":'100', "error":"none"},{"name":"two", "number":'200', "error":"none"},{"name":"three", "number":'300', "error":"found"},{"name":"four", "number":'400', "error":"none"}]}]}
if (testJSON.cluster.length != 0)
{
$.each(testJSON.cluster, function(i, clstr)
{
$('.clusters').append('<ul class="nodes">');
$.each(clstr.node, function(i, ndes)
{
$.find('ul').append('<li>'+ndes.name+'</li>');
});
$('.clusters').append('</ul>');
});
}
What did I manage do do wrong here? I can't see it, through the first $.each it works, get to the second it stops..
var testJSON = {"cluster":[{"node":[{"name":"one", "number":'100', "error":"none"},{"name":"two", "number":'200', "error":"none"},{"name":"three", "number":'300', "error":"found"},{"name":"four", "number":'400', "error":"none"}]}]}
if (testJSON.cluster.length != 0)
{
$.each(testJSON.cluster, function(i, clstr)
{
$('.clusters').append('<ul class="nodes">');
$.each(clstr.node, function(i, ndes)
{
$.find('ul').append('<li>'+ndes.name+'</li>');
});
$('.clusters').append('</ul>');
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将代码更改为以下内容:
附加元素时,不必稍后附加结束标记。另外,
find
不能直接从 jQuery 对象调用。你需要一个选择器。Change your code to the following:
When you append an element, you don't have to later append the closing tag. Also,
find
cannot be called directly from the jQuery object. You need a selector.什么是
$.find
,您在内循环中遇到异常,然后停止。您是否正在寻找在外循环中添加的
ul
?如果是,则使用What is
$.find
, you are getting an exception in the inner loop and then it stops.Are you looking for the
ul
which you added in the outer loop? If yes, then use