jQuery移动元素丢失该元素

时间:2018-08-31 12:24:16

标签: jquery dom

我正在构建移动导航。桌面版本具有相同的元素,但位置不同。它将被包裹在一个cms周围,不允许控制导航标记,因此我只能通过父母操作<ul>

单击第一层菜单项时,我要使用其子菜单并将其插入覆盖主导航的新div中。这个新的div有一个后退按钮,“。remove()”是新的div。

一切正常。问题是,如果我再次单击该项目,会出现新的div,但子菜单不见了。原因是我将其从菜单的原始位置删除,并将其放置在新元素中。当新元素被销毁时,子菜单也将被销毁。

这是我的代码: http://jsfiddle.net/6w17vu2z/261/

$(document).ready(function() {
  var search_toggle = $('.ch-form_toggle'),
    search_tray = $('.ch-form_tray');

  search_toggle.on('click touch', function() {
    if (search_tray.hasClass('open')) {
      search_tray.removeClass('open');
      search_tray.addClass('close');
    } else {
      search_tray.addClass('open');
      search_tray.removeClass('close');
    }
  });

  var pre_header = $('.ch-pre_head'),
    nav_wrap = $('.ch-nav_wrap'),
    search_form = $('.ch-search_form');

  nav_wrap.prepend($('<div></div>').addClass('ch-mob_nav_head'));

  var mob_head = $('.ch-mob_nav_head');

  mob_head.prepend(pre_header, search_form);

  nav_wrap.append($('<ul></ul>').addClass("ch-mob_pre"));

  var mob_pre = $('.ch-mob_pre');

  mob_pre.append(
    pre_header.find($('ul li:nth-child(1)')),
    pre_header.find($('ul li:nth-child(2)'))
  );

  var first_tier = $('.ch-navigation > ul > li > a');

  first_tier.on('click touch', function(e) {
    e.preventDefault();
    var inner_text = $(this).html(),
      parent_li = $(this).parents('li'),
      sibling_ul = parent_li.find('> ul'),
      nav_shell = $(this).parents('.ch-navigation');

    nav_shell.append($('<div></div>').addClass('ch-sub_menu_tray'));

    var sub_menu_tray = $('.ch-sub_menu_tray');

    sub_menu_tray.append($('<a></a>')
      .addClass("ch-return")
      .attr({
        href: '#'
      })
      .html("< Main Menu"), $('<h3></h3>').html(inner_text), sibling_ul);

    sub_menu_tray.height(nav_shell.outerHeight(true) + mob_pre.outerHeight(true));
  });

  $(document).on('click touch', '.ch-return', function(e) {
    e.preventDefault();
    var parent_tray = $(this).parents('.ch-sub_menu_tray'),
      sibling_menu = parent_tray.find('ul');

    sibling_menu.appendTo();
    console.log(parent_tray);
    parent_tray.remove();

  });

  /*$('.ch-return').on('click touch', function(e) {
  	e.preventDefault();
  	var btn_parent	= $(this).parents('.sub_menu_tray');
    
    btn_parent.remove();
  });*/
});
@keyframes search {
  0% {
    visibility: visible;
  }
  90% {
    transform: scale(1.1);
    opacity: 1;
    visibility: visible;
  }
  100% {
    transform: scale(.9);
    opacity: 1;
    visibility: visible;
  }
}

body,html{margin:0;padding:0;}

.cass-header {
  width: 100%;
  max-width: 100%;
    -webkit-box-shadow: 0 0px 5px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
  box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
  position: relative;
}

.cass-header a {
  color: #000;
  font-family: sans-serif;
  font-size: 16px;
  line-height: 1;
  font-weight: 300;
  text-decoration: none;
}

.ch-page_center {
    max-width: 1350px;
    width: 96%;
    margin: 0 auto;
}

.ch-pre_head {
  padding: 3px 0;
  border-bottom: 1px solid #efefef;
}

.ch-pre_head ul {
    margin: 0;
    padding: 0;
    display: block;
    text-align: right;
    list-style: none;
}

.ch-pre_head ul li {
  display: inline-block;    
}

.ch-pre_head ul li a {
  display: inline-block;
  text-align: center;
  padding: 4px 15px;
  font-size: 16px;
}

.ch-pre_head ul li:last-of-type a {
    background: #000;
    border-radius: 8px;
    color: #fff;
}

.ch-main_head {
  display: flex;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: -o-flex;
  
  padding: 15px 0;
}

.ch-logo_wrap,
.ch-logo_wrap a,
.ch-logo_wrap a img {
    display: block;
    width: 150px;
}

.ch-nav_wrap {
  display: flex;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: -o-flex;
  
  align-items: center;
  -webkit-align-items: center;
  
  justify-content: space-between;
  -webkit-justify-content: space-between;
  -moz-justify-content: space-between;
  
  margin: 0 0 0 auto;
}

.ch-navigation ul {
  display: flex;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: -o-flex;
  

  display: block;
  margin: 0;
  padding: 0;
  text-align: right;
  list-style: none;
}

.ch-navigation ul li {
  position: relative;
}

.ch-navigation > ul > li {
  display: inline-block;
}

.ch-navigation > ul > li > a {
  display: inline-block;
  padding: 5px 25px;
}

.ch-navigation > ul > li > ul,
.ch-navigation > ul > li > ul > li > ul {
  position: absolute;
  top: 26px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1100;
  
  padding: 10px 0;
  width: 180px;
  background: #fff;
  -webkit-box-shadow: 0 0px 5px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
  box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
  
  opacity: 0;
  visibility: hidden;
  max-height: 0;
  
  transition: .2s ease all;
  -webkit-transition: .2s ease all;
  -moz-transition: .2s ease all;
  -o-transition: .2s ease all;
}

.ch-navigation > ul > li > ul > li > ul {
  top: 0;
  left: 100%;
  transform: translateX(0);
  z-index: 1101;
}

.ch-navigation > ul > li:hover > ul,
.ch-navigation > ul > li > ul > li:hover > ul {
  opacity: 1;
  visibility: visible;
  max-height: 1000px;
  
  transition: .2s ease all;
  -webkit-transition: .2s ease all;
  -moz-transition: .2s ease all;
  -o-transition: .2s ease all;
}

.ch-navigation > ul > li > ul > li {}

.ch-navigation > ul > li > ul > li > a,
.ch-navigation > ul > li > ul > li > ul > li > a {
  display: block;
  width: 100%;
  padding: 8px 0;
  font-size: 14px;
  line-height: 1;
  font-weight: 300;
  text-align: center;
}

.ch-search_form {
  position: relative;
}

.ch-form_toggle {
  cursor: pointer;
}

.ch-form_toggle,
.ch-form_toggle img {
    width: 27px;
}

.ch-form_tray {
  position: absolute;
  bottom: -40px;
  right: 0;
  padding: 5px;
  width: 218px;
  background: #fff;
  border-radius: 8px 6px;
  
  -webkit-box-shadow: 1px 1px 5px 0px rgba(0,0,0,0.75);
  -moz-box-shadow: 1px 1px 5px 0px rgba(0,0,0,0.75);
  box-shadow: 1px 1px 5px 0px rgba(0,0,0,0.75);
  
  opacity: 0;
  visibility: hidden;
  transform: scale(0);
}

.ch-form_tray.open {
  animation: search .2s linear forwards;
}

.ch-form_tray form {
  display: block;
  width: 100%;
  position: relative;
}

.ch-form_tray form:before {
  content: "";
  position: absolute;
  top: -18px;
  right: 3px;
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 12px solid #aaa;
}

@media (max-width: 860px) {
  .ch-nav_wrap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    background: #fff;
    flex-direction: column;
    -webkit-flex-direction: column;
    -moz-flex-direction: column;
    -ms-flex-direction: column;
    -o-flex-direction: column;
    -webkit-box-shadow: 0 0px 5px 0px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
    box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75);
  }
  .ch-nav_wrap .ch-pre_head ul {
    
  }
  .ch-navigation {
    order: 2;
    -webkit-order: 2;
    -moz-order: 2;
    -ms-order: 2;
    -o-order: 2;
    width: 100%;
    position: relative;
  }
  .ch-navigation ul,
  .ch-navigation ul li ul,
  .ch-navigation ul li ul li ul {
    max-width: 100%;
    width: 100%;
    position: static;
    transform: translateX(0);
  }
  .ch-navigation ul li ul,
  .ch-navigation ul li ul li ul {
    padding: 0;
  }
  .ch-navigation ul li,
  .ch-navigation ul li ul li,
  .ch-navigation ul li ul li ul li {
    display: block;
  }
  .ch-navigation > ul > li:hover > ul,
  .ch-navigation > ul > li > ul > li:hover > ul {
    opacity: 0;
    visibility: hidden;
    max-height: 0;
  }
  .ch-navigation > ul > li.open > ul,
  .ch-navigation > ul > li > ul > li.open > ul {
    opacity: 1;
    visibility: visible;
    max-height: 1000px;
    padding: 10px 0;
  }
  .ch-navigation ul li a,
  .ch-navigation ul li ul li a,
  .ch-navigation ul li ul li ul li a {
    display: block;
    width: 90%;
    margin: 0 auto;
    border-bottom: 1px solid #000;
    text-align: left;
    padding: 10px 0;
    font-size: 16px;
    font-weight: 500;
  }
  .ch-sub_menu_tray {
    position: absolute;
    top: 0;
    left: 0;
    left: 0;
    bottom: 0;
    z-index: 1102;
    width: 100%;
    background: #fff;
  }
  
  .ch-sub_menu_tray ul ul {
    display: none;
  }
  
  
  .ch-mob_nav_head {
    width: 100%;
    max-width: 90%;
    margin: 20px auto;
  }
  .ch-mob_nav_head:after {
    content: "";
    display: table;
    clear: both;
  }
  .ch-mob_nav_head .ch-pre_head {
    display: inline-block;
    float: left;
    margin-right: 15px;
  }
  .ch-search_form {
   float: left;
  }
  .ch-mob_pre {
    order: 3;
    -webkit-order: 3;
    -moz-order: 3;
    -ms-order: 3;
    -o-order: 3;
    
    padding: 0;
    margin: 0;
    list-style: none;
    text-align: left !important;
    width: 100%;
    max-width: 90%;
    margin: 20px auto;
  }
  .ch-mob_pre .ch-pre_head {
    display: inline-block;
    width: auto;
    border: 0;
  }
  .ch-mob_pre li {
    display: inline-block;
  }
  .ch-mob_pre li a {
    display: inline-block;
    margin-right: 30px;
  }
  .ch-mob_pre li a:after {
    content: '>';
    margin-left: 10px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cass-header">
  <div class="ch-page_center">
    <div class="ch-pre_head">
      <ul>
        <li><a href="#">Investors</a></li>
        <li><a href="#">Careers</a></li>
        <li><a href="#">Login</a></li>
      </ul>
    </div>
    <div class="ch-main_head">
      <div class="ch-logo_wrap">
        <a href="/">
          <img src="https://www.cassinfo.com/images/logo.jpg" alt="">
        </a>
      </div>
      <div class="ch-nav_wrap">
        <div class="ch-navigation">
          <ul>
            <li>
              <a href="#">Solutions</a>
              <ul>
                <li>
                  <a href="#">Placeholder Link 1</a>
                  <ul>
                    <li><a href="#">Placeholder Link</a></li>
                    <li><a href="#">Placeholder Link</a></li>
                    <li><a href="#">Placeholder Link</a></li>
                  </ul>
                </li>
                <li>
                  <a href="#">Placeholder Link 2</a>
                  <ul>
                    <li><a href="#">Placeholder Link</a></li>
                    <li><a href="#">Placeholder Link</a></li>
                    <li><a href="#">Placeholder Link</a></li>
                  </ul>
                </li>
                <li><a href="#">Placeholder Link 3</a></li>
              </ul>
            </li>
            <li>
              <a href="#">Customers</a>
              <ul>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
              </ul>
            </li>
            <li>
              <a href="#">About us</a>
              <ul>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
              </ul>
            </li>
            <li>
              <a href="#">Resources</a>
              <ul>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
              </ul>
            </li>
            <li>
              <a href="#">Contact Us</a>
              <ul>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
                <li><a href="#">Placeholder Link</a></li>
              </ul>
            </li>
          </ul>
        </div>
        <div class="ch-search_form">
          <div class="ch-form_toggle">
            <img src="http://newharvest.ca/wordpress/wp-content/uploads/2012/12/icon_grey_seo.png" alt="">
          </div>
          <div class="ch-form_tray">
            <form>
              <input type="search" placeholder="Search...">
              <input type="submit" value="Go">
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

有没有办法从导航中借用子菜单,以便根据需要保持可用状态?还是将其移出位置但将其存储为变量?有几个子菜单和子子菜单,所以我看不到变量起作用。

1 个答案:

答案 0 :(得分:1)

.clone()有效。我用过:

sibling_ul.clone().appendTo(sub_menu_tray);

我要将子菜单的克隆版本附加到新的div。新的div和clone被销毁,但原始子菜单仍然保持原样。