JavaScript中的JavaScript移动元素 [英] JavaScript moving element in the DOM

查看:100
本文介绍了JavaScript中的JavaScript移动元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在页面上有三个< div> 元素。如何交换第一个和第三个< div> 的位置? jQuery很好。

解决方案

使用 jQuery

  $('#div1')。insertAfter('#div3 ); 
$('#div3')。insertBefore('#div2');

如果要重复执行,您需要使用不同的选择器,因为div将保留他们的ids被移动了。

  $(function(){
setInterval(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中的JavaScript移动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆