使用 Javascript/Jquery 遍历无序列表

发布于 2024-09-07 19:24:42 字数 690 浏览 8 评论 0原文

假设我有一个无序的嵌套列表:

<ul>
   <li>Item a1</li>
   <li>Item a2</li>
   <li>Item a3</li>
       <ul>
           <li>Item b1</li>
           <li>Item b2</li>
           <li>Item b3</li>
            <ul>
               <li>Item c1</li>
               <li>Item c2</li>
               <li>Item c3</li>             
            </ul>
           <li>Item b4</li>
       </ul>
  <li>Item a4</li>
</ul>

我需要遍历它并将其保存在二维数组中(最终我只是想将其转换为 JSON 实体)我可以同时使用Jquery 和/或 Javascript。我应该如何进行?

谢谢

Lets say I have an unordered nested list:

<ul>
   <li>Item a1</li>
   <li>Item a2</li>
   <li>Item a3</li>
       <ul>
           <li>Item b1</li>
           <li>Item b2</li>
           <li>Item b3</li>
            <ul>
               <li>Item c1</li>
               <li>Item c2</li>
               <li>Item c3</li>             
            </ul>
           <li>Item b4</li>
       </ul>
  <li>Item a4</li>
</ul>

I need to traverse it and save it in a two dimensional array (ultimately I'm just trying to convert it into a JSON entity) I am allowed to use both Jquery AND/OR Javascript. How should I proceed?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几味少女 2024-09-14 19:24:42

我不确定您想要的结果数据结构是什么样子,但是这个(使用一些 jQuery):

$(function() {

    var result = {};

    function createNewLevel(parent,items) {
        var length = items.length;
        for(var i = 0; i < length; i++) {
            if(items[i].tagName == 'UL') {
                parent['ul' + i] = {};
                createNewLevel(parent['ul' + i],$(items[i]).children().get());
            } else {
                parent['li' + i] = $(items[i]).text();
            }
        }
    }

    createNewLevel(result, $('ul:first').get());

    console.log(result);

});

... 会产生这个结构

{
    ul0: {
        li0: "Item a1",
        li1: "Item a2",
        li2: "Item a3",
        li4: "Item a4",
        ul3: {
            li0: "Item b1",
            li1: "Item b2",
            li2: "Item b3",
            li4: "Item b4",
            ul3: {
                li0: "Item c1",
                li1: "Item c2",
                li2: "Item c3"
            }
        }
    }
}

如果需要的话,可以很容易地对其进行调整以改变结果结构的细节。

请注意,这是一个 JavaScript 对象。如果你确实需要一个 JSON 对象,那么你只需要使用 var jsonResult = JSON.stringify( result ); 来转换它

I'm not sure exactly what you want the resulting data structure to look like, but this (which uses some jQuery):

$(function() {

    var result = {};

    function createNewLevel(parent,items) {
        var length = items.length;
        for(var i = 0; i < length; i++) {
            if(items[i].tagName == 'UL') {
                parent['ul' + i] = {};
                createNewLevel(parent['ul' + i],$(items[i]).children().get());
            } else {
                parent['li' + i] = $(items[i]).text();
            }
        }
    }

    createNewLevel(result, $('ul:first').get());

    console.log(result);

});

... would produce this structure

{
    ul0: {
        li0: "Item a1",
        li1: "Item a2",
        li2: "Item a3",
        li4: "Item a4",
        ul3: {
            li0: "Item b1",
            li1: "Item b2",
            li2: "Item b3",
            li4: "Item b4",
            ul3: {
                li0: "Item c1",
                li1: "Item c2",
                li2: "Item c3"
            }
        }
    }
}

It could be fairly easily tweaked to alter details of the resulting structure if needed.

Please note that this is a javascript object. If you actually need a JSON object, then you just need to convert it using var jsonResult = JSON.stringify( result );

蝶舞 2024-09-14 19:24:42
function traversing(ul)
{
     for(var index=0;index<ul.childNodes.length;index++){
          if(ul.childNodes[index].childNodes.length>0){
              traversing(ul.childNodes[index]);
          }
          //perform other operation
     }
}
function traversing(ul)
{
     for(var index=0;index<ul.childNodes.length;index++){
          if(ul.childNodes[index].childNodes.length>0){
              traversing(ul.childNodes[index]);
          }
          //perform other operation
     }
}
烟织青萝梦 2024-09-14 19:24:42

使用 JQuery 尝试:

$.each($("li"), function(index, value) { 
  alert(value); 
});

With JQuery try:

$.each($("li"), function(index, value) { 
  alert(value); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文