简体   繁体   English

使用ID交换两个DIV的位置

[英]Swap Position of two DIV using ID

i'm trying to change the position of two DIV'S in jquery, it seems that i may be a little confuse because it is already swapping position, but when it do, it duplicates the field. 我正在尝试更改jquery中两个DIV的位置,似乎我可能有点困惑,因为它已经在交换位置,但是当这样做时,它会重复该字段。

i tried using insertBefore and insertAfter. 我尝试使用insertBefore和insertAfter。

<div id="before_stops">                                         
   <li><span><img src="<?php echo base_url('assets/images/multi_stops.png') ?>" width="18px"height="18px"/></span>                                             
          <div class="verticalLine"></div>                                         
            <span class="text multi_non_append_stops"></span>                                               
             <br><br>                                            
    </li>                                    
 </div>                                         
<div id="multi_stops">                                           
   <span class="text multi">
     <li><span>
     <img src="<?php echo base_url('assets/images/EndPoint.png')?>" width="18px" height="18px"></span>
     <div class="verticalLine"></div>
    <br/><br>
    </li>
</span>
</div>

Here is my javascript code: 这是我的JavaScript代码:

for (var i = 0; i < data.stops.length; i++) {
  if(value == "A" || value == "B"){
                        $('#multi_stops').insertBefore($('#before_stops'));
                        $('.multi').append('<li><span><img src="' + base_url + 'assets/images/multi_stops.png" width="18px" height="18px"></span><div class="verticalLine"></div>' + data.stops[i].stopDestination + "<br/><br></li>");
}else{
                        $('#multi_stops').insertAfter($('#before_stops'));
                        $('.multi').append('<li><span><img src="' + base_url + 'assets/images/multi_stops.png" width="18px" height="18px"></span><div class="verticalLine"></div>' + data.stops[i].stopDestination + "<br/><br></li>");
                    }
                }

what is happening is instead of having a output of 发生的事情不是输出

if the value is A or B 如果值为AB

it should be <div id ="multi_stops"></div> <div id ="before_stops"><div> 它应该是<div id ="multi_stops"></div> <div id ="before_stops"><div>

but what is happening to me is <div id ="multi_stops"></div> <div id ="before_stops"><div> <div id ="multi_stops"></div> 但是发生在我身上的是<div id ="multi_stops"></div> <div id ="before_stops"><div> <div id ="multi_stops"></div>

else if the value is not A or B it should only be <div id ="before_stops"></div> <div id ="multi_stops"><div> 否则,如果该值不是AB ,则应仅为<div id ="before_stops"></div> <div id ="multi_stops"><div>

but what is happening is <div id ="before_stops"></div> <div id ="multi_stops"><div> <div id ="multi_stops"></div> 但是发生了什么事<div id ="before_stops"></div> <div id ="multi_stops"><div> <div id ="multi_stops"></div>

it is duplicating. 它正在复制。 what am i doing wrong here. 我在这里做错了什么。

You need to copy the element, insert it after, then delete the first one. 您需要复制元素,然后将其插入,然后删除第一个。 Or you would end up with duplicates. 否则您最终会重复。 You could use jquerys detach method. 您可以使用jquery 分离方法。 It removes the object but it keeps the data. 它删除对象,但保留数据。 Use it like this: 像这样使用它:

for (var i = 0; i < data.stops.length; i++) {
    if (value == "A" || value == "B") {
        $('#multi_stops').detach().insertBefore($('#before_stops'));
        $('.multi').append('<li><span><img src="' + base_url + 'assets/images/multi_stops.png" width="18px" height="18px"></span><div class="verticalLine"></div>' + data.stops[i].stopDestination + "<br/><br></li>");
    } else {
        $('#multi_stops').detach().insertAfter($('#before_stops'));
        $('.multi').append('<li><span><img src="' + base_url + 'assets/images/multi_stops.png" width="18px" height="18px"></span><div class="verticalLine"></div>' + data.stops[i].stopDestination + "<br/><br></li>");
    }
}

Try this: 尝试这个:

<div class="holder">
    <div id="before_stops"> 
    </div>
    <div id="multi_stops">  
    </div>
</div>

if(value == "A" || value == "B"){
    $("#before_stops").appendTo(".holder");
}else{
    $("#multi_stops").appendTo(".holder");
}

Live example: https://codepen.io/noelelias/pen/bGbwwBz 实时示例: https//codepen.io/noelelias/pen/bGbwwBz

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

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