简体   繁体   English

如何将div附加到另一个div

[英]How to append a div to another div

Now I have multiples PHP for loop in my project exactly the same like one below but with different PHP variables of course. 现在,我的项目中有多个PHP for循环,与下面的示例完全相同,但当然具有不同的PHP变量。 I also have an add button with appends a div element. 我还有一个添加按钮,它附加了div元素。 The problem is because I have many for-loop and each for-loop has an add button so when I click on one add button of a particular div it appends a new div to all. 问题是因为我有很多for循环,每个for循环都有一个添加按钮,因此当我单击特定div的一个添加按钮时,它将向所有div添加一个新的div。

<div class="appendOuter">
 <?php 
     foreach($query as $data){
     $id = $data['ID'];
     $initialAccess = $data['Access'];
     if(strlen($initialAccess) > 3){
     echo   '
          <div class="box" >
            <input type="hidden" value='.$id.' class="ID">
             <textarea class="Access">'.$Access.'</textarea>
         </div>';
             }
         }
 ?>
    <div class="text-center">
       <button class="btn btn-success btn-add" type="button" onclick="addMorediv(this)">
       <span class="glyphicon glyphicon-plus"></span>
       </button>
      </div>      
</div>

My javascript to append a new div 我的JavaScript追加一个新的股利

<script>
function addMorediv(index){
  var element =$("<div class='box'><textarea class='Access'></textarea><i class='far fa-flag'></i></div>");
  element.appendTo('.appendOuter');      
 }
  </script>

假设所有其他HTML结构与您演示的结构类似:

element.appendTo($(this).parent().parent());

Are you looking for something like this? 您是否正在寻找这样的东西? Where when you click the add button, it adds a new text area under the existing textarea (s) already within that row? 就是当你点击添加按钮,它增加了现有的下一个新的文本区域textarea (S)已经内该行? This is a jQuery specific answer, and doesn't require the "onclick" of button that you were using. 这是jQuery特定的答案,不需要您使用的按钮的"onclick" It is also adding your content within a parent "row" div for ease of access navigating the DOM during the click event. 它还将您的内容添加到父"row" div ,以便在单击事件期间轻松访问DOM。 So in your PHP for loop, you would echo out 1 of the rows in my example, using your PHP variables in the appropriate places like in your example. 因此,在您的PHP for循环中,您将在示例中的适当位置使用PHP变量来回显示例中的1行。

 $('.btn-add').on('click', function() { var $box = $(this).parent().parent().find('.box:last'); var $element = $("<div class='box'><textarea class='Access'></textarea><i class='far fa-flag'></i></div>"); $element.appendTo($box); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="appendOuter"> <!-- Assume your PHP generated 3 ".row" div rows in for loop --> <div class="row"> <div class="box"> <input type="hidden" value="1" class="ID"> <textarea class="Access">1</textarea> </div> <div class="text-center" style="padding:10px;"> <button class="btn btn-success btn-add" type="button"> <span class="glyphicon glyphicon-plus">Add +</span> </button> </div> </div> <div class="row"> <div class="box"> <input type="hidden" value="2" class="ID"> <textarea class="Access">2</textarea> </div> <div class="text-center" style="padding:10px;"> <button class="btn btn-success btn-add" type="button"> <span class="glyphicon glyphicon-plus">Add +</span> </button> </div> </div> <div class="row"> <div class="box"> <input type="hidden" value="3" class="ID"> <textarea class="Access">3</textarea> </div> <div class="text-center" style="padding:10px;"> <button class="btn btn-success btn-add" type="button"> <span class="glyphicon glyphicon-plus">Add +</span> </button> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM