从ul读取数据到不同的ul

时间:2017-05-16 20:39:49

标签: jquery

我试图读出我的导航栏,然后使用jQuery将其放入另一个ul

不确定怎么做。我尝试使用.each& .children

从导航中读取(此列表):

<ul class="navbar-nav">
    <li><a class="nav-item nav-link" href="materiaal.html">Materiaal</a></li>
    <li><a class="nav-item nav-link" href="muziek.html">Muziek</a></li>
    <li><a class="nav-item nav-link" href="mixing1.html">Mixing Pt. 1</a></li>
    <li><a class="nav-item nav-link" href="mixing2.html">Mixing Pt. 2</a></li>
    <li><a class="nav-item nav-link" href="performing.html">Performing</a></li>
</ul>

将它们列入此列表:

<ul class="navigatie"></ul>

2 个答案:

答案 0 :(得分:2)

无需单独处理这些元素。您可以clone()整个批次,append()将结果发送到另一个ul

$('.navbar-nav > li').clone().appendTo('.navigatie')
ul {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="navbar-nav">
  <li><a class="nav-item nav-link" href="materiaal.html">Materiaal</a></li>
  <li><a class="nav-item nav-link" href="muziek.html">Muziek</a></li>
  <li><a class="nav-item nav-link" href="mixing1.html">Mixing Pt. 1</a></li>
  <li><a class="nav-item nav-link" href="mixing2.html">Mixing Pt. 2</a></li>
  <li><a class="nav-item nav-link" href="performing.html">Performing</a></li>
</ul>

<ul class="navigatie"></ul>

答案 1 :(得分:1)

您可以使用html()获取ul。

的内容

试试这个:

 $(".navigatie").html($(".navbar-nav").html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="navbar-nav">
            <li><a class="nav-item nav-link" href="materiaal.html">Materiaal</a></li>
            <li><a class="nav-item nav-link" href="muziek.html">Muziek</a></li>
            <li><a class="nav-item nav-link" href="mixing1.html">Mixing Pt. 1</a></li>
            <li><a class="nav-item nav-link" href="mixing2.html">Mixing Pt. 2</a></li>
            <li><a class="nav-item nav-link" href="performing.html">Performing</a></li>
        </ul>
        <br><br>
        <ul class="navigatie"></ul>