JavaScript 在 DOM 中移动元素 [英] JavaScript moving element in the DOM
本文介绍了JavaScript 在 DOM 中移动元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在一个页面上有三个
元素.如何交换第一个和第三个
的位置?jQuery 很好. 解决方案 Trivial with jQuery
$('#div1').insertAfter('#div3');$('#div3').insertBefore('#div2');
如果您想重复执行此操作,则需要使用不同的选择器,因为 div 在四处移动时会保留其 ID.
$(function() {设置间隔(函数(){$('div:first').insertAfter($('div').eq(2));$('div').eq(1).insertBefore('div:first');}, 3000 );});
Let's say I have three <div>
elements on a page. How can I swap positions of the first and third <div>
? jQuery is fine.
解决方案 Trivial with jQuery
$('#div1').insertAfter('#div3');
$('#div3').insertBefore('#div2');
If you want to do it repeatedly, you'll need to use different selectors since the divs will retain their ids as they are moved around.
$(function() {
setInterval( function() {
$('div:first').insertAfter($('div').eq(2));
$('div').eq(1).insertBefore('div:first');
}, 3000 );
});
这篇关于JavaScript 在 DOM 中移动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文