jquery动态添加元素,报错。请问这个问题怎么处理。

yyxu123 yyxu123 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
firebug 提示错误如下。HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy
我的目的是想动态的做一棵树。


<html>
<head>
<script type="text/javascript" src="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/jquery/jquery-1.10.2.min_f2fb5194.js"></script>
</head>
<body>
<div id="nestable_dep" class="nestable-white dd"></div>


<script type="text/javascript">
jQuery(document).ready(function() {
var Depelm = $("#nestable_dep");

var data=[{"SystemUsers":[],"Id":1,"Code":"123","Name":"123","ParentId":1,"Description":"","State":1},{"SystemUsers"
:[],"Id":2,"Code":"1232","Name":"123","ParentId":1,"Description":"","State":1},{"SystemUsers":[],"Id"
:3,"Code":"12321","Name":"1231","ParentId":2,"Description":"","State":1},{"SystemUsers":[],"Id":4,"Code"
:"2343","Name":"2343","ParentId":2,"Description":"","State":1},{"SystemUsers":[],"Id":5,"Code":"222"
,"Name":"222","ParentId":2,"Description":"","State":1},{"SystemUsers":[],"Id":6,"Code":"qwe","Name":"www"
,"ParentId":2,"Description":"","State":1}]

var dom = document.createElement("ol");
$(dom).addClass("dd-list");
Depelm.append(dom);

$.each(data, function(index, value) {

var item = document.createElement("li");
$(item).addClass("dd-item").attr("data-id", value.Id);
var itemDiv = document.createElement("div");
$(itemDiv).addClass("dd-handle").text(value.Name);
$(item).append(itemDiv);

var hasChild = Depelm.find("[data-id='" + value.ParentId + "']");
if (hasChild.length > 0) {
var cdol = $(hasChild[0]).children("ol:first");
if (cdol.length > 0) {
$(cdol[0]).append(item);
} else {
var cpol = $(hasChild[0]);
cpol.append(dom);
cpol.children("ol:first").append(item);
}
} else {
Depelm.children("ol:first").append(item);
}

});

});
</script>
</body>
</html>
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
yyxu123
JavaScript 87,984 成员 224,680 内容