【问题标题】:Unable to insert element before and after (both) in jQuery无法在jQuery中(两者)之前和之后插入元素
【发布时间】:2020-10-16 11:26:31
【问题描述】:

我正在使用 jQuery 3.x。我正在尝试使用insertBefore()insertAfter() 在元素之前和之后附加一个动态创建的元素。但是,只有insertBefore() 工作,而另一个被忽略。当我评论一个时,另一个正在工作。为什么?

p = $("<p></p>").text("This is a dynamicly created element");
p.insertAfter($('nav'));
p.insertBefore($('nav'));
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 20px;
}

header,
nav,
main,
aside,
footer {
  padding: 10px 15px;
  border: 1px solid mediumseagreen;
  text-align: center;
}

header {
  background: dodgerBlue;
}

nav {
  background: mediumSeaGreen;
}

main {
  background: #d3d3d3;
}

main,
aside {
  height: 1200px;
}

main {
  width: 80%;
  float: left;
}

aside {
  width: 20%;
  float: right;
}

div::after {
  content: " ";
  float: none;
  clear: both;
  display: table;
}

main {
  text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
  This is header
</header>

<nav>
  This is navbar
</nav>

<main>
  <article>
    <h2>This is heading</h2>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Cum atque fuga, eos neque ipsum enim id inventore necessitatibus laboriosam quo nobis, repellendus maxime veritatis error ut expedita, velit aspernatur asperiores!
    </p>
  </article>
</main>

<aside>
  This is side bar
</aside>
<div></div>
<footer>
  This is <a href="http://">footer</a>
</footer>

【问题讨论】:

    标签: javascript jquery dom


    【解决方案1】:

    问题是因为p 仅引用单个元素。在 insertAfter() 调用中将其插入到 DOM 中,然后使用 insertBefore() 将同一元素移动到新位置。

    要执行您需要的操作,您可以在第二次插入之前clone() 元素。另请注意,您不需要创建整个 jQuery 对象来选择nav,您可以将选择器作为字符串传递。试试这个:

    let p = $("<p />", { 
      text: "This is a dynamicly created element"
    });
    p.insertAfter('nav');
    p.clone().insertBefore('nav');
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-size: 20px;
    }
    
    header,
    nav,
    main,
    aside,
    footer {
      padding: 10px 15px;
      border: 1px solid mediumseagreen;
      text-align: center;
    }
    
    header {
      background: dodgerBlue;
    }
    
    nav {
      background: mediumSeaGreen;
    }
    
    main {
      background: #d3d3d3;
    }
    
    main,
    aside {
      height: 1200px;
    }
    
    main {
      width: 80%;
      float: left;
    }
    
    aside {
      width: 20%;
      float: right;
    }
    
    div::after {
      content: " ";
      float: none;
      clear: both;
      display: table;
    }
    
    main {
      text-align: left;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <header>This is header</header>
    <nav>This is navbar</nav>
    <main>
      <article>
        <h2>This is heading</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Cum atque fuga, eos neque ipsum enim id inventore necessitatibus laboriosam quo nobis, repellendus maxime veritatis error ut expedita, velit aspernatur asperiores!
        </p>
      </article>
    </main>
    
    <aside>This is side bar</aside>
    <div></div>
    <footer>This is <a href="http://">footer</a></footer>

    还有一点需要提一下,我建议研究一下 flexbox 布局。与强制 display: tablediv 上创建多列布局相比,它们是一种更现代且可扩展的技术。

    【讨论】:

    • 但是,我认为元素只是字符串,在 javascript 中作为文本创建。我说的对吗?
    • 在这种情况下,没有。您从&lt;p /&gt; 字符串创建一个jQuery 对象。然后,您多次引用该 single 对象,这就是它被移动的原因。
    • 我可以使用 typeof 运算符获取它的类型,也可以通过再次初始化 var p 来插入它。
    猜你喜欢
    • 2015-01-16
    • 2016-03-12
    • 1970-01-01
    • 2011-03-15
    • 2011-01-16
    • 2012-08-26
    • 1970-01-01
    • 2015-06-14
    • 2014-04-28
    相关资源
    最近更新 更多