Javascript: removeChild 在特定屏幕尺寸

Javascript: removeChild at certain screen size

我已经尝试了几个小时,以在特定屏幕尺寸(>60em 和 <90em)下围绕两个 div(aside 和 .related)添加一个包装器。我正在用 matchMedia 和一个 eventListener 来做这件事。包装似乎添加在正确的位置,但问题是即使不满足尺寸条件,它仍然存在。

这是一个 jsfiddle:http://jsfiddle.net/Vanilla__/4q26ngmg/1/

简化HTML:

<body>
  <header>Header</header>
  <main>Main</main>
  <aside>Aside</aside>
  <div class="related">Related</div>
  <footer>Footer</footer>
</body>

Javascript:

if(window.matchMedia("screen and (min-width: 60em) and (max-width: 90em)").matches) {

  window.addEventListener("resize", function addWrapper(q) {

  //Create div with id wrapper
  var div = document.createElement('div');
  div.id = "wrapper";

  // Select aside
  var selectDiv = document.querySelector("aside");

  //clone
  div.appendChild(selectDiv.cloneNode(true));
  //Place the new wrapper at the right place in the HTML
   selectDiv.parentNode.replaceChild(div, selectDiv);

  //Add related to the wrapper so they're both in the wrapper
  document.querySelector('#wrapper').appendChild(
    document.querySelector('.related') );

  });
}

我想添加一个 'else' 以在有另一个屏幕尺寸时删除子项(使用 removeChild)或删除 eventListener(使用 removeEventListener),但我得到的只是关于函数未定义的错误或其他错误,无论我尝试什么。

else {
       window.removeEventListener("resize", addWrapper(q));
    }

有谁知道当屏幕尺寸不大于 60em 且小于 90em 时如何去除包装纸?我是 Javascript 菜鸟(可能很清楚 ;))。感谢任何帮助。

你可以这样做:

var addWrapper = function () {

    //Don't add wrapper if already added
    var wrapper = document.getElementById("wrapper");
    if (wrapper !== null) return;

    //Create div with id wrapper
    var div = document.createElement('div');
    div.id = "wrapper";

    // Select aside
    var selectDiv = document.querySelector("aside");

    //clone
    div.appendChild(selectDiv.cloneNode(true));
    //Place the new wrapper at the right place in the HTML
    selectDiv.parentNode.replaceChild(div, selectDiv);

    //Add related to the wrapper so they're both in the wrapper
    document.querySelector('#wrapper').appendChild(
    document.querySelector('.related'));
};

var removeWrapper = function () {
    //Don't remove if there is no wrapper
    var wrapper = document.getElementById("wrapper");
    if (wrapper === null) return;

    //Replace wrapper with its content
    wrapper.outerHTML = wrapper.innerHTML;
}

var wrapperFixer = function () {
    if (window.matchMedia("screen and (min-width: 60em) and (max-width: 90em)").matches) {
        addWrapper();
    } else {
        removeWrapper();
    }
}

window.onload = function () {
    window.addEventListener("resize", wrapperFixer);
    //Check and add if wrapper should be added on load
    wrapperFixer();
}
body {
    display: flex;
    height: 40em;
    flex-wrap: wrap;
    font-family: Helvetica, Arial, sans-serif;
    color: white;
    text-align: center;
}
header {
    background-color: purple;
    width: 30%
}
main {
    background-color: pink;
    width: 40%
}
aside {
    background-color: deepPink;
    width: 15%
}
.related {
    background-color: red;
    width: 15%
}
footer {
    background-color: slateBlue;
    width: 100%;
    height: 5em;
}
#wrapper {
    border: 4px solid white;
}
<body>
    <header>Header</header>
    <main>Main</main>
    <aside>Aside</aside>
    <div class="related">Related</div>
    <footer>Footer</footer>
</body>