我怎么能用jquery提升DOM(how can I move up the DOM with jquery)

我有非常低的jQuery知识,所以我有一个问题..

我想知道你是否可以在jQuery的帮助下移动一些选择器div 。

以下是我目前的DOM

<div class="header"> <div class="logo"> ... </div> <!-- end .logo --> <div class="menu"> ... </div> <!-- end .menu --> </div>

但我想将.menu div容器移动到比.logo div容器更上面的容器

<div class="header"> <div class="menu"> ... </div> <!-- end .menu --> <div class="logo"> ... </div> <!-- end .logo --> </div>

有没有办法在jQuery的帮助下做到这一点。

i have very low jQuery knowledge so thats y I have a question..

I wonder if you can move some selector div with the help of jQuery.

Following is my current DOM

<div class="header"> <div class="logo"> ... </div> <!-- end .logo --> <div class="menu"> ... </div> <!-- end .menu --> </div>

but i want to move .menu div container to above than .logo div container like that

<div class="header"> <div class="menu"> ... </div> <!-- end .menu --> <div class="logo"> ... </div> <!-- end .logo --> </div>

is there a possible way to do this with the help of jQuery.

最满意答案

有一百种方法可以做到这一点。 这是我最可读的一个:

$('.menu').insertBefore('.logo');

小提琴

或者,如果.menu应始终是.menu的第一个子.header独立于.logo ,您可以使用:

$('.menu').prependTo('.header');

小提琴

There is a hundred ways to do this. Here's the one I just most readable:

$('.menu').insertBefore('.logo');

Fiddle

Alternatively, if .menu should always be the first child of .header independently of .logo, you can use:

$('.menu').prependTo('.header');

Fiddle

更多推荐