Uncaught HierarchyRequestError:追加节点失败

Uncaught HierarchyRequestError: Failed to append the node

本文关键字:节点 失败 追加 HierarchyRequestError Uncaught      更新时间:2023-09-26

我在向特定节点追加内容时遇到问题。这是我的Jsfidde http://jsfiddle.net/usezpkj2/7/

我有一个主列表

Project1

Project2

在Project1和project2中,隐藏的<ul class=hiddendivcontent中有一些列表项,当点击glyhicon-chevron-down时,列表将显示出来。下面是它的代码:

$(".glyphicon ").click( function ( e ){ 
   e.preventDefault() // prevent default action - hash doesn't appear in url
    var target = $(e.target);
    if(target.hasClass("glyphicon-chevron-right")){
    $(e.target).toggleClass('glyphicon-chevron-right glyphicon-chevron-down');
    
    $(this).closest("li").append($('.hiddendivcontent'));
        $(this).parent().children(".hiddendivcontent").toggle();
    }
    else if(target.hasClass("glyphicon-chevron-down")){
         $(e.target).toggleClass('glyphicon-chevron-right glyphicon-chevron-down');
         $(this).parent().children(".hiddendivcontent").toggle();
        
    }
    
} );

当我点击列表项Menu1时,它应该再次追加<ul class="hiddendivcontent">..</ul>作为Menu1内的子。最后我的输出应该是

Project1
   Menu1
     Menu1
     Menu2
   Menu2
Project2

问题是,当我点击菜单,我得到一个异常:

Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node': The new child element contains the parent. 

问题在于追加符()。但是我不知道如何解决它。我是Jquery和Javascript的新手。谁能给我提些建议呢?

问题正是错误所描述的。你想添加:

<ul class="hiddendivcontent">
    <li class="listvalue">Menu1</li>
    <li class="listvalue">Menu2</li>
</ul>

自身内部。如果你想添加另一个看起来相同的html块,你可以这样做:

$(this).closest("li").append($('<ul class="NewList"> <li class="listvalue">Menu3</li></ul>'));

小提琴:http://jsfiddle.net/25q40a87/