将现有UL附加到现有的先前LI

时间:2018-01-29 11:39:03

标签: jquery append siblings

我正在尝试将现有的ul移动到位于li之前的现有ul

<ul class="sub-menu" aria-labelledby="desktop-submenu">
  <li class="has-children current-page-ancestor current-page-parent">
    <a href="/improve-clinical-care/clear/" class="current_page_item">Clear</a>
  </li>
  <ul class="sub-sub-menu">
    <li class="level-three">
      <a href="/improve-clinical-care/clear/clears-story/" class="current_page_item">Clear's Story</a>
    </li>
    <li class="level-three current-menu-item page-item-34 current_page_item">
      <a href="/improve-clinical-care/clear/sign-up-for-clear/" class="current_page_item">Sign-Up for Clear<span class="sr-only">(current)</span></a>
    </li>
  </ul>
  <li class="has-children critical-care">
    <a href="/improve-clinical-care/critical-care/" class="current_page_item">Critical Care</a>
  </li>
  <ul class="sub-sub-menu">
    <li class="level-three">
      <a href="/improve-clinical-care/critical-care/pain-agitation-delirium/" class="current_page_item">Pain Agitation &amp; Delirium</a>
    </li>
    <li class="level-three">
      <a href="/improve-clinical-care/critical-care/glycemic-control/" class="current_page_item">Glycemic Control</a>
    </li>
    <li class="level-three">
      <a href="/improve-clinical-care/critical-care/critical-care-working-group/" class="current_page_item">Critical Care Working Group</a>
    </li>
  </ul>
</ul>

我要做的是将<ul class="sub-sub-menu">项移到前面的<li class="has-children">,因为我们显然不能将ul作为另一个ul的孩子}}

这甚至可以用jQuery吗?我已经接近append()方法了,但由于has-children出现的次数很多,我最终得到了has-children个LI中的一个sub-sub-menu附在它上面。

对此的任何帮助都将非常感激。谢谢帮派!

1 个答案:

答案 0 :(得分:0)

我已经解决了我自己的问题 - 呜呜!

$(".sub-sub-menu").each(function() {
    $(this).prev(".has-children").append(this);
});

显然我已经非常接近了很多次,但却失去了prev而不是siblingsclosest