dongxi1879 2013-05-06 14:20
浏览 229
已采纳

jQuery:获取对象内的元素

So, I've got this code:

function popAreaTree() {

        var tree = $("ol.tree");
        var list1 = tree.children('li');
        var list2 = list1.children('ol').children('li');


        $(tree).on('click', $(this).children('li').children('a'), function(e, i) {
            console.log($(this).text());
            $(this).parent('li').toggleClass('open', 300);
        });

        $.ajax({
            url: '../admin/callbacks/jsonDataList.php',
            data: {t: 'zoneandarea'},
            dataType:"json",
            async: false,
            global: false,
            success: function(j, status) {
                for(var i = 0;1 < j.length; i++) {

                    var li = tree.append('<li class="zone"><a href="#">'+j[i].name+'</a></li>');
                    var areas = '';
                    $.each(j[i].areas, function(i, item) {
                        areas += '<li class="area" data-area-uid="'+item.uid+'">'+item.name+'</li>';
                    });
                    $(areas).insertAfter(li.closest('a')); // This is the problem
                }
            }
        });
    }

It is just basically creating a file tree. The ajax posts to a php page that outputs a json array with one more array inside each result. It goes like this:

array('name'=>name, 'uid'=>uid, areas=>array('name'=>name, 'uid'=>uid))

I just need to know how to do an insertAfter on the a tag within the li object.

  • 写回答

2条回答

  • douzhendi4559 2013-05-06 14:25
    关注

    You could just create an html string and append that to your tree object:

    var html = '<li class="zone"><a href="#">'+j[i].name+'</a>';
    $.each(j[i].areas, function(i, item) {
        html += '<li class="area" data-area-uid="'+item.uid+'">'+item.name+'</li>';
    });
    html += '</li>'; //close first li element
    
    tree.append(html);
    

    As an aside, your issue has to due with your usage of closest, which looks for the closest parent that matches the selector. You would want to use children which just searches through the direct children of the element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮