在第n个位置的div中的AppendChild元素

时间:2015-03-23 12:34:47

标签: jquery

基本上我有三个div元素,我想在<P>之后附加子元素<div class="div1"></div>

我的代码是:

<div class="div1">
            <p></p>
            <section class="section"></section>
 </div>
<div  class="div2"></div>

我试过这个

$('.div1').appendChild('<p>hello world!!</p>');

2 个答案:

答案 0 :(得分:1)

应为.after()

$('.div1').after('<p>hello world!!</p>');

&#13;
&#13;
$('.div1').after('<p>new p</p>');
&#13;
div{border:red solid 1px;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div1">
  <p>inner p in div1 </p>
  <section class="section">inner section in div 1</section>
</div>
<div class="div2">div 2</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

$('.div1+.div2').before('<p>hello world!!</p>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="div1">
            <p>test1</p>
            <section class="section"></section>
 </div>
<div  class="div2">
     <p>test2</p>
    
</div>