简体   繁体   English

从嵌套的div中移出特定的div

[英]Move a particular div out of nested divs

I'm using bootstrap with nested divs but i can't find an efficient way to move out the particular div from the nest for mobile view. 我正在使用带有嵌套div的bootstrap,但我无法找到一种有效的方法从移动视图中移出嵌套中的特定div。 i only find a way with css by doing display block when pc view and display none in mobile display view and vice versa to resolved this, but it will do a bad request from database. 我只能通过在移动显示视图中查看和显示无显示块时找到css的方式,反之亦然解决这个问题,但是它会从数据库执行错误的请求。

(I only know a CSS-Tricks method which creates 2 C divs at different positions.) (我只知道一个CSS-Tricks方法,它在不同的位置创建2个C div。)

default 默认

.c1{
display:block}
.c2{
display:none}

media queries mobile 媒体查询移动

.c1{
display:none}
.c2{
display:block}

updated progress: 更新进度:

$(document).ready(function(){
      var ravenous = function() { 
            if (window.matchMedia('(max-width: 768px)').matches){
                $(".wrap_content").remove();
                $("<div class='extraDIv'></div>").appendTo(".events_wrap");
                $("<div class='top_mid col-lg-12 followmini'>as</div>").appendTo(".extraDiv");
                }
            else{
                $(".extraDIv").remove();
                $(".wrap_content").appendTo(".events_wrap");
                }
  };
    $(window).resize(ravenous);
    ravenous();  

}); });

it's just creating new dives i think, it's not moving the dives 它只是在创造新的潜水,我认为,它不会移动潜水

any other better option to do this? 还有其他更好的选择吗? (meaning, i want the only 1 C div, but it is a moving div. From parent div, moving outside and become an independent div) (意思是,我想要唯一的1 C div,但它是一个移动的div。从父div,移动到外面并成为一个独立的div)

图片链接

Lets say You almost there. 让我们说你几乎在那里。 You didn't post HTML so I'll do simple structure. 你没有发布HTML所以我会做简单的结构。 With matchMedia it will look more like that: 使用matchMedia它看起来更像是:

HTML: HTML:

<div class="wrapper">
    <div class="a">
        <div class="b">
            <div class="c">This is C</div>
        </div>
    </div>
</div>
<div class="mobile_wrapper"></div>

JavaScript: JavaScript的:

var media_query = [
    window.matchMedia('(max-width: 768px)')
    // here You can add other medias
],
    // this is for cache purpouse
    main_wrapper = $('.wrapper'),
    mobile_wrapper = $('.mobile_wrapper'),
    nested_div = $('.c');

// this is pretty obvious I think
function media0(mq){
    if(mq.matches){
        nested_div.appendTo(mobile_wrapper); //  move c into other wrapper
    }else{
        nested_div.appendTo(main_wrapper); // move c into main wrapper
    }
}

media0(media_query[0]); // this is required to execute matchMedia first time
media_query[0].addListener(function(mq){ // event listener which is way more offective than resize() event
    media0(mq);
});

Working example: https://jsfiddle.net/1vtk3tue/1/ I added background color so You can see change. 工作示例: https//jsfiddle.net/1vtk3tue/1/我添加了背景颜色,以便您可以看到更改。

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

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