gpt4 book ai didi

jQuery 在父级中移动 DOM 元素

转载 作者:行者123 更新时间:2023-12-03 22:44:09 26 4
gpt4 key购买 nike

是否有一种简单的方法可以将元素移动到其父元素内?

像这样...

来自:

<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

JSFiddle Demo

关于jQuery 在父级中移动 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com