如何使元素移动到另一个元素然后单击,返回到JQuery中的上一个位置?

时间:2014-12-03 11:15:18

标签: javascript jquery html5

目标

  • 使用什么方法使第3项移动到另一个容器。
  • 然后,如果再次点击,则将该项目返回到上一个位置
  • 此功能应适用于所有项目。

Jquery的

 $('#item3').click(function(){
       // What method to use to make item_1 move to another container.
       // Then if click again return the item to previous position
 });

enter image description here

2 个答案:

答案 0 :(得分:1)

检查DEMO

HTML

<div id="start">
<div class="element">one</div>
<div class="element">two</div>
<div class="element">three</div>
</div>

<div id="target"></div>

的jQuery

$('.element').click(function() {
var cont = $(this).parent().attr('id');
if (cont == 'start') {
    var place = '#target';
} else {
    var place = '#start';
}
$(place).append($(this));
});

答案 1 :(得分:1)

您可以使用jquery UI的drag and drop插件。

如果你想要使用它,那么你可以通过onClick finction尝试这个代码....

.JS

function onclickIteam(iteamId){
    var x = if('#iteamId').html();
    if(if('#iteamId').parent().prop("id") == "my_inventory"){
        //$('#iteamId').detach();
        $('#server_inventory').append(x);
    }else{
        //$('#iteamId').detach();
        $('#my_inventory').append(x);
    }
`}`