最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new

programmeradmin2浏览0评论

Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.

I just try to append span9 before span3 when windows width is less than 767 and I have this error, why is that?

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.content');
  var $cats = $('.span3');

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<script src=".11.0/jquery.min.js"></script>
<div class="content-entry">
  <div class="row">
    <div class="span3">span3</div>
    <div class="span9 content">span9/content</div>
  </div>
</div>

Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.

I just try to append span9 before span3 when windows width is less than 767 and I have this error, why is that?

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.content');
  var $cats = $('.span3');

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="content-entry">
  <div class="row">
    <div class="span3">span3</div>
    <div class="span9 content">span9/content</div>
  </div>
</div>

Share Improve this question edited Jun 6, 2016 at 8:17 T.J. Crowder 1.1m200 gold badges2k silver badges1.9k bronze badges asked Jun 6, 2016 at 7:47 Rav_gRav_g 1231 gold badge2 silver badges13 bronze badges 5
  • With the code and markup in the question, that error doesn't occur. So clearly there's more markup (which isn't surprising). – T.J. Crowder Commented Jun 6, 2016 at 7:53
  • Does not seems to throw error. Can you create Snippet with issue? – Justinas Commented Jun 6, 2016 at 7:53
  • Side note: Look at your checkWidth function and ask yourself what it does if the width is exactly 767. ;-) – T.J. Crowder Commented Jun 6, 2016 at 7:53
  • Add snippet and yes there no error :( – Rav_g Commented Jun 6, 2016 at 8:11
  • "I just try to append span9 before span3 when windows width is less then 767 and i have this error why is that ?" Please don't randomly mark up non-code as code (much less marking up part of a word as code). (I've fixed it for you.) – T.J. Crowder Commented Jun 6, 2016 at 8:16
Add a ment  | 

1 Answer 1

Reset to default 2

Although the error doesn't occur with what you've shown, the error message is quite clear: Apparently, you have at least one element with the class span3 that's inside an element with the class content, like this:

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.content');
  var $cats = $('.span3');

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<div class=".content-entry">
  <div class="row">
    <div class="content"><!-- Note this new element -->
        <div class="span3">span3</div>
    </div>
    <div class="span9 content">span9</div>
  </div>
</div>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

The solution is to target your selectors more carefully at just the elements you want to move around. Apparently content and/or span3 are used for other things as well as what you're using them for here.

For instance:

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.move-this');     // Changed the class
  var $cats = $('.relative-to-this'); // Changed the class

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<div class=".content-entry">
  <div class="row">
    <div class="content"><!-- Note this new element -->
        <div class="span3 relative-to-this">span3</div><!-- Added a class -->
    </div>
    <div class="span9 content move-this">span9</div><!-- Added a class -->
  </div>
</div>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论