gpt4 book ai didi

javascript - 树中的子节点需要设置验证只能添加6层

转载 作者:行者123 更新时间:2023-11-30 19:34:44 25 4
gpt4 key购买 nike

我想限制用户,所以他们最多只能添加 6 级子节点。目前他们可以添加任意数量。

enter image description here

// Add Child
function AddChild(element) {
var ul = element.parentNode.nextElementSibling;
var li = document.createElement("li");
domString = '<div class="textbox_cls"> \n\
<span class="caret"></span>\n\
<span class="remove_user" parent_id="1">-</span>\n\
<input type="text" name="username" placeholder="Element">\n\
<span class="add_user" parent_id="1" onclick="AddChild(this);">+</span>\n\
<span class="drag_user">::</span>\n\
</div><ul></ul>';
li.innerHTML = domString;
ul.appendChild(li);

}
input[type='text'] {
padding: 8px 10px;
border: 1px solid #e5e5e5;
color: gray;
border-radius: 4px;
font-size: 16px;
}

.add_user {
background: green;
color: #fff;
margin: 4px 0px 4px 5px;
}

.remove_user {
background: red;
color: #fff;
line-height: 20px !important;
margin: 4px 5px 4px 0px;
}

.add_user,
.remove_user {
width: 25px;
height: 25px;
display: inline-block;
text-align: center;
line-height: 25px;
border-radius: 50%;
}

.drag_user {
color: lightgrey;
line-height: 30px;
font-weight: bold;
font-size: 30px;
letter-spacing: -2px;
margin: 0px 5px;
}

.add_user,
.remove_user {
font-size: 25px;
font-weight: bold;
}

.User_listing ul {
margin-bottom: 15px;
list-style-type: none;
}

.User_listing ul .textbox_cls {
margin-bottom: 10px;
display: inline-flex;
position: relative;
}

.add_user,
.remove_user,
.drag_user {
cursor: move;
}

.User_listing>ul ul {
margin-left: 10px;
}

.User_listing>ul li {
margin: 0;
padding: 0 0px;
line-height: 20px;
color: #369;
font-weight: bold;
border-left: 2px dashed rgb(0, 0, 0);
}

.User_listing>ul li:last-child {
border-left: none;
}

.User_listing>ul li:before {
position: relative;
top: -20px;
height: 30px;
width: 33px;
color: white;
border-bottom: 2px dashed rgb(0, 0, 0);
content: "";
display: inline-block;
left: 0px;
}

.User_listing>ul li:last-child:before {
border-left: 2px dashed rgb(0, 0, 0);
}
<html>

<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>

<body>
<div class="User_listing">
<ul>
<li>
<div class="textbox_cls" total_ul="1">
<span class="caret"></span>
<span class="remove_user" parent_id="1">-</span>
<input type="text" name="username" placeholder="Element" />
<span class="add_user" parent_id="1" onclick="AddChild(this);">+</span>
<span class="drag_user">::</span>
</div>
<ul> </ul>
</li>

</ul>
</div>

</body>

</html>

最佳答案

在定义您的 +span 中,我添加了一个自定义属性 - level 并首先将其设置为 1。然后,在 AddChild 中,我使用 getAttribute 获取此属性,将其解析为 int 并递增它。然后在 domString 中再次传递相同的属性。

每次用户尝试添加子项时,我都会检查嵌套级别是否最多为 6。

// Add Child
function AddChild(element) {
var ul = element.parentNode.nextElementSibling;
var li = document.createElement("li");
var level = parseInt(element.getAttribute("level")) + 1;
if (level <= 6) {
domString = '<div class="textbox_cls"> \n\
<span class="caret"></span>\n\
<span class="remove_user" parent_id="1">-</span>\n\
<input type="text" name="username" placeholder="Element">\n\
<span class="add_user" level=' + level + ' parent_id="1" onclick="AddChild(this);">+</span>\n\
<span class="drag_user">::</span>\n\
</div><ul></ul>';
li.innerHTML = domString;
ul.appendChild(li);
}
}
input[type='text'] {
padding: 8px 10px;
border: 1px solid #e5e5e5;
color: gray;
border-radius: 4px;
font-size: 16px;
}

.add_user {
background: green;
color: #fff;
margin: 4px 0px 4px 5px;
}

.remove_user {
background: red;
color: #fff;
line-height: 20px !important;
margin: 4px 5px 4px 0px;
}

.add_user,
.remove_user {
width: 25px;
height: 25px;
display: inline-block;
text-align: center;
line-height: 25px;
border-radius: 50%;
}

.drag_user {
color: lightgrey;
line-height: 30px;
font-weight: bold;
font-size: 30px;
letter-spacing: -2px;
margin: 0px 5px;
}

.add_user,
.remove_user {
font-size: 25px;
font-weight: bold;
}

.User_listing ul {
margin-bottom: 15px;
list-style-type: none;
}

.User_listing ul .textbox_cls {
margin-bottom: 10px;
display: inline-flex;
position: relative;
}

.add_user,
.remove_user,
.drag_user {
cursor: move;
}

.User_listing>ul ul {
margin-left: 10px;
}

.User_listing>ul li {
margin: 0;
padding: 0 0px;
line-height: 20px;
color: #369;
font-weight: bold;
border-left: 2px dashed rgb(0, 0, 0);
}

.User_listing>ul li:last-child {
border-left: none;
}

.User_listing>ul li:before {
position: relative;
top: -20px;
height: 30px;
width: 33px;
color: white;
border-bottom: 2px dashed rgb(0, 0, 0);
content: "";
display: inline-block;
left: 0px;
}

.User_listing>ul li:last-child:before {
border-left: 2px dashed rgb(0, 0, 0);
}
<html>

<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>

<body>
<div class="User_listing">
<ul>
<li>
<div class="textbox_cls" total_ul="1">
<span class="caret"></span>
<span class="remove_user" parent_id="1">-</span>
<input type="text" name="username" placeholder="Element" />
<span class="add_user" level=1 parent_id="1" onclick="AddChild(this);">+</span>
<span class="drag_user">::</span>
</div>
<ul> </ul>
</li>

</ul>
</div>

</body>

</html>

关于javascript - 树中的子节点需要设置验证只能添加6层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com