在2个框之间移动元素
Moving elements between 2 boxes
我已经研究了关于这个主题的相同问题,但不知何故,建议的解决方案对我不起作用:/
问题是div只从#box1移动到#box2一次。如果使用了detach(),那么div在#box2中是可点击的,但在点击时会被重新排列。如果remove()使用的div在#box2中根本不可点击(事件监听器消失了?)。我有一种感觉,移动div的过程在某种程度上并不完全,我认为DOM中有重复的div或移动的div完全消失,对点击没有反应。
我尝试了各种组合的detachment()、remove()和appendTo(),我能得到的最好的结果是在下面
http://jsfiddle.net/uoz3t914/13/
$('#box1 .item' ).on('click', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).remove().appendTo('#box2');
});
$('#box2 .item' ).on('click', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).appendTo('#box1');
});
在您的情况下,您必须使用事件委派
$('#box1' ).off().on('click','.item', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).appendTo('#box2');
});
$('#box2' ).off().on('click', '.item', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).appendTo('#box1');
});
您将事件附加到父级,父级将其传播到子级,然后在附加事件的任何时候,放置一个off()
来分离它。
$('#box1' ).off().on('click','.item', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).appendTo('#box2');
});
$('#box2' ).off().on('click', '.item', function() {
// $( this ).detach().appendTo('#box2'); moves divs around in #box2
$( this ).appendTo('#box1');
});
.item {
width: 50px;
height: 50px;
float: left;
}
#box1 {
border: 1px dotted blue;
position: relative;
width: 200px;
height: 100px;
}
#i1 {
background-color: yellow;
}
#i2 {
background-color: green;
}
#i3 {
background-color: red;
}
#box2{
border: 1px solid black;
width: 250px;
height: 100px;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="box1">
<div class ="item" id ="i1"></div>
<div class ="item" id ="i2"></div>
<div class ="item" id ="i3"></div>
</div>
<div id = "box2">
</div>
您可以使用在框之间移动它们
$('#box1, #box2').on('click', '.item', function () {
$(this).appendTo($(this).parent().prop('id') == 'box1' ? '#box2' : '#box1');
});
$('#box1, #box2').on('click', '.item', function () {
$(this).appendTo($(this).parent().prop('id') == 'box1' ? '#box2' : '#box1');
});
.item {
width: 50px;
height: 50px;
float: left;
}
#box1 {
border: 1px dotted blue;
position: relative;
width: 200px;
height: 100px;
}
#i1 {
background-color: yellow;
}
#i2 {
background-color: green;
}
#i3 {
background-color: red;
}
#box2 {
border: 1px solid black;
width: 250px;
height: 100px;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="box1">
<div class="item" id="i1"></div>
<div class="item" id="i2"></div>
<div class="item" id="i3"></div>
</div>
<div id="box2"></div>
这使用.on()
的事件委派语法来处理元素,并使用三元运算符来确定元素存在于哪个框中。
使用此html:
<div id="wrapper">
<div id ="box1" class="container">
<div class ="item" id ="i1"></div>
<div class ="item" id ="i2"></div>
<div class ="item" id ="i3"></div>
</div>
<div id = "box2" class="container">
</div>
</div>
和这个javascript
$('.item').on('click', function(){
var index = $("#wrapper > .container").index($(this).parent()),
maxIndex = $('#wrapper > .container').length,
nextIndex = (index + 1) < maxIndex ? (index + 1) : 0;
$(this).appendTo($('#wrapper > .container').eq(nextIndex));
});
可以在任意数量的集装箱之间移动箱子
您还可以在"#wrapperdiv"中添加Box3、Box4(带有class.contator)等,您可以动态地进行
相关文章:
- 在2个框之间移动元素
- I'我试图在我的网页上动态地上下移动元素.我该怎么做
- 如何通过箭头键连续/平滑地移动元素
- 我应该怎么做才能避免在网页上移动元素(html、css、jQuery)
- 在firefox上的滚动事件中移动元素
- Javascript-通过按下另一个元素来移动元素
- 如何在Javascript中移动元素的旋转点
- 调整大小时移动元素
- 如何通过JS移动元素位置而不闪烁
- 如何前后移动元素
- j查询调整大小时移动元素
- 在元素重叠时移动元素
- 是否可以使用 css 拖动属性来移动元素而不会掉落
- 使用箭头键移动元素 jquery
- 字典中由元素:位置对组成的移动元素
- 保留列表中移动元素的顺序
- 使用具有正确光标定位的 Javascript 移动元素
- 使用 CSS 在容器内移动元素
- 使用 Jquery .after() 来移动元素
- 如何在 JavaScript 中移动
元素