简体   繁体   English

wrap()多次重复元素-jQuery

[英]wrap() duplicates element more than once - jQuery

I have a pretty basic function which allows me to wrap a div tag, inside of another div tag, whenever the browser window becomes larger than x amount of pixels. 我有一个非常基本的功能,每当浏览器窗口大于x像素数量时,我就可以在另一个div标签内包装一个div标签。

However, I have a strange bug which occurs once the page is resized more than once. 但是,我有一个奇怪的错误,一旦页面被多次调整大小,就会发生。

For starters, I'll show you the jQuery script which I am currently using, so you can get a better idea of what I am working with. 首先,我将向您展示当前正在使用的jQuery脚本,以便您可以更好地了解自己的工作方式。

$(window).on("resize", function(){
    if (window.matchMedia("(min-width: 1920px)").matches) {
        $(".navbar-inner").wrap("<div id='center'></div>");
    } else {
        var content = $("#center").contents();
        $("#center").replaceWith(content);
    }
}).trigger("resize");

Basically, once the window becomes larger than 1919px , it will force the navbar to be wrapped inside of a new div tag. 基本上,一旦窗口大于1919px ,它将迫使导航栏被包装在新的div标签内。

The problem with this is when the page is resized more than once, the <div id='center'> element will be duplicated multiple times. 问题是当页面调整大小超过一次时, <div id='center'>元素将被重复多次。

So, rather than the output in the dom being something like: 因此,而不是dom中的输出是这样的:

<div id="center">
    <div class="navbar-inner">
        <!-- e.t.c... !-->
    </div>
</div>

It will be: 这将是:

<div id="center">
    <div id="center">
        <div id="center">
            <div id="center">
                <!-- this will duplicate over and over until at some point, it displays the navbar-inner div !-->
                <div class="navbar-inner">
                    <!-- e.t.c... !-->
                </div>
            </div>
        </div>
    </div>
</div>

I have searched almost every single thread, link, answer I could find, but I simply cannot find something that will fix my issue. 我搜索了几乎所有可以找到的单个线程,链接和答案,但是我根本找不到能解决问题的内容。

I'm pretty new to JavaScript, so all help is appreciated. 我对JavaScript非常陌生,因此不胜感激。

Cheers. 干杯。

You need to check inside if condition that it's already wrapped or not by checking the count of element. 您需要通过检查元素计数来检查内部是否已包装的条件。 Also you can use unwrap() to unwarp the element. 您也可以使用unwrap unwrap()来取消扭曲元素。

$(window).on("resize", function() {
  if (window.matchMedia("(min-width: 1920px)").matches) {
    if (!$('#center').length)
      $(".navbar-inner").wrap("<div id='center'></div>");
  } else {
    $("#center .navbar-inner").unwrap();
  }
}).trigger("resize");   

or check the parent is #center using is() method. 或使用is()方法检查父对象是否为#center

$(window).on("resize", function() {
  if (window.matchMedia("(min-width: 1920px)").matches) {
    if (!$(".navbar-inner").parent().is("#center"))
      $(".navbar-inner").wrap("<div id='center'></div>");
  } else {
    $("#center .navbar-inner").unwrap();
  }
}).trigger("resize");

You can check if the element is already wrapped and act accordingly: 您可以检查元素是否已经包装并采取相应措施:

$(window).on("resize", function(){
    if (window.matchMedia("(min-width: 1920px)").matches) {
        if( $(".navbar-inner").closest("#center").length == 0 )
        {
           $(".navbar-inner").wrap("<div id='center'></div>");
        }
    } else {
        var content = $("#center").contents();
        $("#center").replaceWith(content);
    }
}).trigger("resize");

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

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