是否有一种简单的方法可以将元素移动到其父元素内?
像这样...
来自:
<div class="test">hello 1</div>
<div class="test">hello 2</div>
<div class="test">hello 3</div>
至:
<div class="test">hello 1</div>
<div class="test">hello 3</div>
<div class="test">hello 2</div>
将 div 向上或向下移动一步,或者使用索引在特定位置追加。
最佳答案
您应该能够使用.prev()
和 .next()
像这样(这里是一个 jQuery 函数):
$.fn.moveUp = function() {
$.each(this, function() {
$(this).after($(this).prev());
});
};
$.fn.moveDown = function() {
$.each(this, function() {
$(this).before($(this).next());
});
};
$("div:eq(2)").moveUp(); //Would move hello 3 up
$("div:eq(0)").moveDown(); //Would move hello 1 down
关于jQuery 在父级中移动 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098257/