如何“剪切”DOM元素并在其他地方显示?

时间:2015-04-01 10:47:39

标签: javascript jquery css html5

我有网站,我想" cut"每div#id来自.tab-content,并且仅当视口宽度小于768px时才将每个div添加到每个li

我的HTML页面:

    <section id="menu">
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <ul id="menu-nav" class="nav nav-pills text-center">
                    <li class="active"><h2><a href="#about" data-toggle="tab">About</a></h2></li>
                    <li><h2><a href="#services" data-toggle="tab">Services</a></h2></li>
                    <li><h2><a href="#contact"  data-toggle="tab">Contact</a></h2></li>
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <div class="tab-content">
                    <div class="tab-pane active" id="about">
                        <div class="col-sm-8 col-sm-offset-2">
                            <p class="text-center">
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis odit id praesentium, maxime, harum corporis officia laboriosam earum laudantium mollitia et quas, iure enim expedita. Nam sit, est libero obcaecati.
                            </p>
                        </div>
                    </div>

                    <div class="tab-pane" id="services">
                        <div class="col-sm-8 col-sm-offset-2">
                            <p class="text-center">
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis odit id praesentium, maxime, harum corporis officia laboriosam earum laudantium mollitia et quas, iure enim expedita. Nam sit, est libero obcaecati.
                            </p>
                        </div>
                    </div>

                    <div class="tab-pane" id="contact">
                        <div class="col-sm-8 col-sm-offset-2">
                            <p class="text-center">
                            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis odit id praesentium, maxime, harum corporis officia laboriosam earum laudantium mollitia et quas, iure enim expedita. Nam sit, est libero obcaecati.
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

我尝试为每个div #ID使用jquery,但它不起作用。

  $(document).ready(function(){

    if ($(window).width() < 768) {
      var divAbout = $('#about').pop();
      $(divAbout).appendTo('#menu-nav li:first-of-type');
    }

  });

我希望在移动设备上获得该结果:

<li>
<h2>About</h2>
<div id="about">
...
</div>
</li>

和其他div一样。

2 个答案:

答案 0 :(得分:2)

希望您能够开发响应式网页设计。而不是削减DOM并将其添加到li标记。使用CSS3 media queries根据元素宽度显示和隐藏元素。

但这是针对您提问的方法:

 $(document).ready(function(){

   if ($(window).width() < 768) {
       $(".tab-content div").each(function(index, ele) {
           var id = this.id; // about , contact
           $("li a[href='#'"+id+"]").parent().parent().append(this);
           // this will match li with a having href="#about" etc
       });
   }

});

答案 1 :(得分:2)

您可以使用调整大小处理程序并执行

var flag;
$(window).on('resize.menu', function () {
    var width = $(window).width(),
        $ul = $('#menu-nav');
    if (width < 768 && flag != 1) {
        flag = 1;
        $('#menu .tab-content .tab-pane').each(function () {
            $(this).appendTo($ul.find('li:has(a[href="#' + this.id + '"])'))
        });
    } else if (width >= 768 && flag != 2) {
        flag = 2;
        $ul.find('.tab-pane').appendTo('#menu .tab-content');
    }
}).trigger('resize.menu')

演示:Fiddle