需限制树形结构子节点最多仅可添加6层级
Limit Tree Node Depth to 6 Levels
Alright, let's fix that unlimited node addition issue and cap your tree structure at 6 levels max. The core idea is to calculate how deep the current node is before allowing a child to be added, then block the action if we've hit the limit.
Step 1: Update the AddChild Function
We'll add logic to count the current node's depth by traversing up the DOM tree. Here's the modified function:
// Add Child function AddChild(element) { // Calculate current depth of the parent node let currentDepth = 1; let parentElement = element.closest('li'); // Traverse up to count nested levels while (parentElement && parentElement.parentElement && parentElement.parentElement.tagName !== 'DIV') { if (parentElement.parentElement.tagName === 'UL') { currentDepth++; } parentElement = parentElement.parentElement.parentElement; } // Block addition if we've reached level 6 if (currentDepth >= 6) { alert("Cannot add more than 6 levels of nodes!"); return; } // Original add logic remains intact 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); // Disable the new node's add button if it would create a 7th level const newAddBtn = li.querySelector('.add_user'); if (currentDepth + 1 >= 6) { newAddBtn.classList.add('disabled'); newAddBtn.onclick = null; } }
Step 2: Add Visual Feedback for Disabled Buttons
To make it obvious when users can't add more nodes, add this CSS rule to gray out disabled "+" buttons:
.add_user.disabled { background: #ccc; cursor: not-allowed; }
Full Updated Code
Here's the complete working code with all changes included:
HTML + CSS + JavaScript
<html> <head> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <style> 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; cursor: pointer; } .add_user.disabled { background: #ccc; cursor: not-allowed; } .remove_user { background: red; color: #fff; line-height: 20px !important; margin: 4px 5px 4px 0px; cursor: pointer; } .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; cursor: move; } .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; } .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); } </style> </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> <script> // Add Child function AddChild(element) { // Calculate current depth of the parent node let currentDepth = 1; let parentElement = element.closest('li'); // Traverse up to count nested levels while (parentElement && parentElement.parentElement && parentElement.parentElement.tagName !== 'DIV') { if (parentElement.parentElement.tagName === 'UL') { currentDepth++; } parentElement = parentElement.parentElement.parentElement; } // Block addition if we've reached level 6 if (currentDepth >= 6) { alert("Cannot add more than 6 levels of nodes!"); return; } // Original add logic remains intact 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); // Disable the new node's add button if it would create a 7th level const newAddBtn = li.querySelector('.add_user'); if (currentDepth + 1 >= 6) { newAddBtn.classList.add('disabled'); newAddBtn.onclick = null; } } </script> </body> </html>
How It Works
- Depth Calculation: We traverse up from the clicked "+" button, counting each nested
<ul>to determine the current node's level. - Limit Enforcement: If the current node is already at level 6, we show an alert and stop the add action.
- Visual Feedback: Nodes at level 5 get their "+" button disabled (gray background, no click action) so users can't accidentally try to add a 7th level.
内容的提问来源于stack exchange,提问作者Hardik Kalathiya
相关产品推荐
相关产品推荐

