gpt4 book ai didi

javascript - 根据点击序列生成字符串路径

转载 作者:行者123 更新时间:2023-12-03 01:37:01 25 4
gpt4 key购买 nike

我根据对象结构生成嵌套的 div 元素。通过单击父项,您可以切换子项。

现在我想生成一个路径,用斜杠分隔单击序列和“选定”元素。当用户单击“阅读”->“新闻”->“体育”时,字符串路径应为“阅读/新闻/体育”。当用户现在点击“阅读”->“书籍”时,路径现在应该是“阅读/书籍”

这是我当前的版本:https://codepen.io/iamrbn/pen/yEqPjG

let path = "";

let object = {
"design": {
"inspiration": {},
"news": {}
},
"read": {
"news": {
"sport": {}
},
"books": {}
},
"code": {}
}

let categoryContainer = document.querySelector(".categoryContainer")

function categoryTree(obj, parent, start = true) {
for (var key in obj) {
let div = document.createElement("div");
div.textContent = key;
div.classList.add("category");
if (parent.children) parent.className += " bold";
if (!start) div.className = "normal hide category";

div.addEventListener('click', function(e) {
e.stopPropagation()
this.classList.toggle('active');
Array.from(div.children).forEach(child => {
child.classList.toggle('hide');
})
})
categoryTree(obj[key], div, false)
parent.appendChild(div)
}
}


categoryTree(object, categoryContainer)
.category {
color: black;
display: block;
line-height: 40px;
background-color: RGBA(83, 86, 90, 0.2);
margin: 8px;
}

.category .category {
display: inline-block;
margin: 0 8px;
padding: 0 8px;
}

.category.hide {display: none;}
.category.normal {font-weight: normal;}
.category.bold {font-weight: bold;}
.category.active {color: red;}
<div class="categoryContainer"></div>

最佳答案

这是一种方法。除了添加对新 getParents() 函数的调用之外,您的现有代码未作任何修改,该函数通过递归爬行 DOM 树来生成单击节点的“路径”:

let path = "";

let object = {
"design": {
"inspiration": {},
"news": {}
},
"read": {
"news": {
"sport": {}
},
"books": {}
},
"code": {}
}

let categoryContainer = document.querySelector(".categoryContainer")

function categoryTree(obj, parent, start = true) {
for (var key in obj) {
let div = document.createElement("div");
div.textContent = key;
div.classList.add("category");
if (parent.children) parent.className += " bold";
if (!start) div.className = "normal hide category";

div.addEventListener('click', function(e) {
e.stopPropagation()
this.classList.toggle('active');
Array.from(div.children).forEach(child => {
child.classList.toggle('hide');
})
var thePath = getParents(e.target); // <--- new
console.log(thePath)
})
categoryTree(obj[key], div, false)
parent.appendChild(div)
}
}

function getParents(node, path) {
// Cheat a bit here: we know the textnode we want is the first child node, so we don't have to iterate through all children and check their nodeType:
let thisName = node.childNodes[0].textContent;
path = path ? (thisName + "/" + path) : thisName ;
// iterate to parent unless we're at the container:
if (node.parentNode.className.split(/\s+/).indexOf("categoryContainer") !== -1) {
return path;
} else {
return getParents(node.parentNode, path);
}
}

categoryTree(object, categoryContainer)
.category {
color: black;
display: block;
line-height: 40px;
background-color: RGBA(83, 86, 90, 0.2);
margin: 8px;
}

.category .category {
display: inline-block;
margin: 0 8px;
padding: 0 8px;
}

.category.hide {
display: none;
}

.category.normal {
font-weight: normal;
}

.category.bold {
font-weight: bold;
}

.category.active {
color: red;
}
<div class="categoryContainer"></div>

关于javascript - 根据点击序列生成字符串路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51024224/

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