简体   繁体   English

在Angular中将一个DIV元素移入另一个

[英]Moving one DIV element inside another in Angular

I am working with Jquery code in Angular where I want to move one DIV element inside another Here is my code 我正在使用Angular中的Jquery代码,我想在其中将一个DIV元素移到另一个中这是我的代码

.ts file .ts文件

moveButton(elem) {
  alert('hello' + elem);
  if ( $(elem).parent().attr('id') === 'oneDiv' ) {
    alert('hello' + elem);
    $(elem).detach().appendTo('#anotherDiv');

  } else {

    $(elem).detach().appendTo('#oneDiv');
  }
}

.html file .html文件

<div id="oneDiv">   
  <button id="btnDefault" class='btn123' (click)='moveButton(this)' type='button'> Button 1  </button>
  <button id="btnPrimary" class='btn123' (click)='moveButton(this)' type='button' > Button 2 </button>
  <button id="btnDanger" class='btn123' (click)='moveButton(this)' type='button' > Button 3 </button>
</div> 
<div id="anotherDiv"> </div>

But My code is not working may be because detach() , parent() , attr() do not work . 但是我的代码不起作用可能是因为detach(),parent()和attr()不起作用。

I'm not expert in angular but your jquery code inside the function works fine for me .. your problem maybe in the relation between the function and (click)='moveButton(this)' 我不是角度专家,但是您在函数内的jQuery代码对我来说很好..您的问题可能是函数与(click)='moveButton(this)'之间的关系

this is how it works with jquery without a function 这是没有功能的 jquery 的工作方式

 $(document).ready(function(){ $(document).on('click' , '.btn123' , function(){ var elem = $(this); if ( $(elem).parent().attr('id') === 'oneDiv' ) { $(elem).detach().appendTo('#anotherDiv'); } else { $(elem).detach().appendTo('#oneDiv'); } }); }); 
 #oneDiv{ background : red; } #anotherDiv{ background : blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="oneDiv"> <button id="btnDefault" class='btn123' type='button'> Button 1 </button> <button id="btnPrimary" class='btn123' type='button' > Button 2 </button> <button id="btnDanger" class='btn123' type='button' > Button 3 </button> </div> <div id="anotherDiv"> </div> 

You don't need to use detach() and appendTo() on same level, only appendTo() function is enough to do this. 您不需要在同一级别上使用detach()appendTo() ,只需执行appendTo()函数即可。

I know you wanted to do this in AngularJS, but you can do this totally in jQuery, check below code: 我知道您想在AngularJS中执行此操作,但是您可以在jQuery中完全执行此操作,请检查以下代码:

 function moveButton(elem) { if ( $(elem).parent().attr('id') == 'oneDiv' ) { $(elem).appendTo('#anotherDiv'); } else { $(elem).appendTo('#oneDiv'); } } 
 #oneDiv{ border:solid 2px blue; padding:20px } #anotherDiv{ border:solid 2px orange; padding:20px; margin-top:10px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="oneDiv"> <button id="btnDefault" class='btn123' onclick='moveButton(this)' type='button'> Button 1 </button> <button id="btnPrimary" class='btn123' onclick='moveButton(this)' type='button' > Button 2 </button> <button id="btnDanger" class='btn123' onclick='moveButton(this)' type='button' > Button 3 </button> </div> <div id="anotherDiv"> </div> 

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

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