解决方案 »

  1.   

    所谓的递归:
    /**
    parseLl:解析li
    @jdom:一个jquery 取出的dom对象
    */
    function parseLi(jdom){
         var a = {};
         jdom.each(function(){
             var that = $(this);
             a['id']  = that.attr('id');
             a['nodes'] = parseUl(that.find('>ul'));
         });
         return a;
    }
    /**
    parseUl:解析ul(假定同时只有一个ul)
    @jdom:一个jquery 取出的dom对象
    */
    function parseUl(jdom){
          if(jdom.length){
          return {
                       id:jdom.attr('id')
                       ,nodes:parseLi(jdom.find('>li');
          }
         }else{
             return {};
         }
    }//假定最外面的ul.id = xxx  假定 ul的子可能包含li   假定 li的子可能包含 ul
    var myjson = parseUl($('#xxx'));
      

  2.   

     function f(count,json) {
            var counts = 0;
            count.each(function() {
            var id = $(this).attr("id");
                json = json + ',{'+'"id"'+':' +'"' +$(this).attr("id")+'",'+'"node"'+':' +'"'+ counts + '"}';
                if ($(this).contents().filter("ul").length != 0) {
                    f($(this).children("ul").children("li"));
                } else {
                    counts += 1;
                }
            })
        }大神哥哥 你来得太晚了 ~ 应该早点出来给我指点啊  这是我想了半天才想出来的~~·