【问题标题】:Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent未捕获的 HierarchyRequestError:无法在“节点”上执行“insertBefore”:新的子元素包含父元素
【发布时间】:2016-06-06 07:47:28
【问题描述】:

未捕获的 HierarchyRequestError:无法在“节点”上执行“insertBefore”:新的子元素包含父元素。

当窗口宽度小于 767 时,我只是尝试在 span3 之前附加 span9 并且出现此错误,这是为什么呢?

$(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.com/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>

【问题讨论】:

  • 使用问题中的代码和标记,that error doesn't occur。所以很明显有更多的标记(这并不奇怪)。
  • 似乎不会抛出错误。您可以创建有问题的片段吗?
  • 旁注:看看你的checkWidth函数,问问自己如果宽度正好是767,它会做什么。;-)
  • 添加 sn-p 是的,没有错误:(
  • “当 windows width 是 less then 767 时,我只是尝试在 span3 之前附加 span9 并且我有这个错误,为什么会这样?” 请不要'不要将非代码随机标记为代码(更不用说将单词的部分标记为代码)。 (我已经为你修好了。)

标签: javascript jquery


【解决方案1】:

虽然您所显示的内容没有发生错误,但错误消息非常清楚:显然,您至少有一个具有 span3 类的元素,该元素班级content,像这样:

$(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.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

解决方案是将选择器更仔细地定位在您想要移动的元素上。显然content 和/或span3 用于其他用途以及您在此处使用它们的用途。

例如:

$(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.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

【讨论】:

  • 试图理解但没有运气..我编辑并放了一张html结构的图片,我没有任何其他具有类内容或span3等的div..
  • @Rav_g:如果您遇到该错误,您确实.content 的实例中拥有.span3 的实例。
  • 为什么这是个问题,@T.J.Crowder?我似乎无法理解这个:S
  • @D'T4ils - 因为如果有.span3 inside .content, $content.insertBefore($cats);/$content.insertAfter($cats); 会尝试在自身内部插入$content 元素。
  • @T.J.Crowder 无论如何让它忽略内部元素?我想插入更广泛的元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多