2013-05-06 14:20
浏览 171


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) {
            $(this).parent('li').toggleClass('open', 300);

            url: '../admin/callbacks/jsonDataList.php',
            data: {t: 'zoneandarea'},
            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+'">''</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.

图片转代码服务由CSDN问答提供 功能建议


  function  popAreaTree(){
 var tree = $(“ol.tree”); 
 var list1 = tree.children('li'); 
 var list2 = list1.children('ol')。children(  'li'); 
 $ .ajax(  {
 url:'.. / admin / callbacks / jsonDataList.php',
 for(var i = 0; 1&lt; j.length; i ++){
 var li = tree.append('&lt; li class =“zone”  &gt;&lt; a href =“#”&gt;'+ j [i] .name +'&lt; / a&gt;&lt; / li&gt;'); 
 var areas =''; 
 $ .each(j [  i] .areas,function(i,item){
 区域+ ='&lt; li class =“area”data-area-uid =“'+ item.uid +'”&gt;'+ +'&lt; / li&gt;'; 
 $( 区域).insertAfter(li.closest( 'A'));  //这是问题

它基本上只是创建了一个文件树。 ajax发布到一个php页面,该页面输出一个json数组,每个结果中还有一个数组。 它是这样的:

  array('name'=&gt; name,'uid'=&gt; uid,areas =&gt; array('name'=&gt; name  ,'uid'=&gt; uid))

我只需要知道如何在 a 标签内执行insertAfter li 对象。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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+'">''</li>';
    html += '</li>'; //close first li element

    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.

    打赏 评论
  • drlu11748 2013-05-06 14:31

    You could look for the anchor inside and then use .after():


    Although, in this case you could also just use .append():

    li.append(areas); // this will go behind <a>
    打赏 评论

相关推荐 更多相似问题