gpt4 book ai didi

jQuery 树遍历 - 将无序列表元素嵌套到 JSON

转载 作者:行者123 更新时间:2023-12-01 00:38:42 24 4
gpt4 key购买 nike

好的,现在我这里有一个无序列表:

<ul id="mycustomid">
<li><a href="url of Item A" title="sometitle">Item A</a>
<ul class="children">
<li><a href="url of Child1 of A" title="sometitle">Child1 of A</a>
<ul class="children">
<li><a href="url of Grandchild of A" title="sometitle">Grandchild of A</a>
<ul class="children">
<li><a href="url of Grand Grand child of A" title="sometitle">Grand Grand child of A</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="url of Item B" title="sometitle">Item B</a></li>
<li><a href="url of Item C" title="sometitle">Item C</a></li>
</ul>

基本上,我只想将此数据转换为 JSON 实体。我想在 jQuery 中完成这项工作,但我认为我很难做到这一点。上面的列表只是一个例子,实际上,我的列表理想情况下应该有更多数量的 child ,并且可能有“n”层深(意思是,它将有孙子的孙子的孙子的孙子的孙子……或更多)我已经失去了无数为此我睡了几个小时,我想我哪儿也去不了:(

我想提取这些内容: anchor 内的文本、 anchor 的 url 和 anchor 的标题,并将它们放入 JSON 实体中

上面列表的 JSON 格式如下:

{
name: "Item A",
url: "url of Item A",
title: "sometitle",
children: [{
name: "Child1 of A",
url: "url of Child1 of A",
title: "sometitle",
children: [{
name: "Grandchild of A",
url: "url of Grandchild of A",
title: "sometitle",
children: [{
name: "Grand Grand child of A",
url: "url of Grand Grand child of A",
title: "sometitle",
children: []
}]
}]
}]
},
{
name: "Item B",
url: "url of Item B",
title: "sometitle",
children: []
},
{
name: "Item C",
url: "url of Item C",
title: "sometitle",
children: []
}

一些有用的引用:

Javascript解决方案: Traversing unordered lists using Javascript/Jquery

^ 这可能有效,但我需要的 JSON 输出格式如上所示,而不是此脚本输出的:(

其他引用:

How do I put unordered list items into an array

https://jsfiddle.net/yS6ZJ/1/

https://jsfiddle.net/CLLts/

https://jsfiddle.net/cWnwt/

请有人帮忙:(
很多个不眠之夜一直让我头疼..(P.s - 我花了大约 40 多分钟才写下整个页面和代码)

最佳答案

啊,一个有趣的小递归练习。我有一个时间来做这件事,这就是我会怎么做的。这可以递归地进行很多级别的深度工作,但假设您的数据不够深,不足以爆炸内存(如果太深,浏览器中的递归会中断)。至少 10 级左右应该没问题。

我对此进行了测试,似乎有效,只需将其保存在 HTML 文件中就可以了。

抱歉,没有太多评论(好吧,从技术上来说,根本没有:),这假设您可以很好地阅读 jQuery 和 JS 代码。如果您有疑问,请在评论中提问,我很乐意为您解释。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Recursive list processor example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

var out = [];

function processOneLi(node) {

var aNode = node.children("a:first");
var retVal = {
"title": aNode.attr("title"),
"url": aNode.attr("href"),
"name": aNode.text()
};

node.find("> .children > li").each(function() {
if (!retVal.hasOwnProperty("children")) {
retVal.children = [];
}
retVal.children.push(processOneLi($(this)));
});

return retVal;
}

$("#mycustomid").children("li").each(function() {
out.push(processOneLi($(this)));
});


console.log("got the following JSON from your HTML:", JSON.stringify(out));

});

</script>
</head>
<body>
<ul id="mycustomid">
<li><a href="http://example.com/urlOfItemA" title="sometitle">Item A</a>
<ul class="children">
<li><a href="http://example.com/urlOfItemAChild1" title="sometitle">Child1 of A</a>
<ul class="children">
<li><a href="http://example.com/urlOfItemAGrandchild" title="sometitle">Grandchild of A</a>
<ul class="children">
<li><a href="http://example.com/urlOfItemAGrandGrandChild" title="sometitle">Grand Grand child of A</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="http://example.com/urlOfItemB" title="sometitle2">Item '"" B</a></li>
<li><a href="http://example.com/urlOfItemC" title="sometitle3">Item C</a></li>
</ul>
</body>
</html>

关于jQuery 树遍历 - 将无序列表元素嵌套到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com