简体   繁体   English

在侧面菜单和导航栏上加载相同的链接

[英]Load same links on side menu and nav bar

Hey I got a navbar and a side menu, both should load the same links (I know it's poor design but it's not up to me).嘿,我有一个导航栏和一个侧面菜单,两者都应该加载相同的链接(我知道这是糟糕的设计,但它不取决于我)。

They work alone, but I can't get them to work the same time.他们单独工作,但我无法让他们同时工作。

This is my navbar这是我的导航栏

<nav class="navbar">
    <a href="home.html"> Home </a>
    <a id="navAnchor"></a>
    <a href=""> Contact </a>
</nav>

This is my side menu这是我的侧边菜单

<div class="sidebar">
    <ul>
        <li><a href="home.html"> Páginal inicial</a></li>
        <li id="listAnchor"></li>
        <li>Contato</li>
    </ul>
</div>

This is the JS to load the links that come front my backend api这是 JS 加载来自我后端的链接 api

const categories = categoriesData.items;

      let list = document.getElementById("listAnchor");
      let navbar = document.getElementById("navAnchor");

      categories.forEach((element) => {
        let link = document.createElement("a");

        link.setAttribute("href", "//localhost:8888");
        link.innerHTML += element.name;

        //Here's the problem, navbar is loaded
        navbar.appendChild(link);
        
        // If I add these lines to load the side menu, navbar is not loaded
        let listItem = document.createElement("li");
        list.appendChild(listItem).appendChild(link);
      });

I've tried to create functions to load them separately, but it didn't work.我试图创建函数来分别加载它们,但没有成功。 Couldn't find anything similar to solve this situation on the inte.net.在 inte.net 上找不到类似的方法来解决这种情况。

Thanks in advance, btw I'm open to any suggestion of what I could do here, I'm kinda new to front end development too.在此先感谢,顺便说一句,我愿意接受任何关于我可以在这里做什么的建议,我对前端开发也有点陌生。

This actually is the expected behaviour.这实际上是预期的行为。 Because you first create element, and than append it to a parent.因为你首先创建了元素,并且比 append 给它一个父元素。 And after that you decide to append it to another parent.之后,您决定将 append 发送给另一位家长。

There are some ways to overcome this, this is the simpliest without changing your entire code.有一些方法可以克服这个问题,这是最简单的方法,无需更改整个代码。

 const categoriesData = { items: [ {name:"test1"}, {name:"test2"}, {name:"test3"}, ] } const categories = categoriesData.items; let list = document.getElementById("listAnchor"); let navbar = document.getElementById("navAnchor"); categories.forEach((element) => { let link = document.createElement("a"); link.setAttribute("href", "//localhost:8888"); link.innerHTML += element.name; //Here's the problem, navbar is loaded navbar.appendChild(link); // If I add these lines to load the side menu, navbar is not loaded let listItem = document.createElement("li"); // Just add.cloneNode function. list.appendChild(listItem).appendChild(link.cloneNode(true)); });
 <nav class="navbar"> <a href="home.html"> Home </a> <a id="navAnchor"></a> <a href=""> Contact </a> </nav> <div class="sidebar"> <ul> <li><a href="home.html"> Páginal inicial</a></li> <li id="listAnchor"></li> <li>Contato</li> </ul> </div>

Adding cloneNode(true) will do the job.添加cloneNode(true)将完成这项工作。 You can get more info fromMozilla Page您可以从Mozilla 页面获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM