简体   繁体   English

Jquery,CSS - 如何在列表中将元素设置为第一个

[英]Jquery, CSS - How to set element as first in list

I dynamically generate a bunch of item, and that looks something like this: 我动态生成一堆项目,看起来像这样:

<div class=gallery_wrapper>
<figure>...</figure>
<figure>...</figure>
<figure>...</figure>
<figure>...</figure>
......
</div>

There is a condition which checks if one of the figure meet the requirements and it then add class to that figure (class=main_img). 有一个条件检查其中一个数字是否符合要求,然后将该类添加到该数字 (class = main_img)。

What I tried is to float figure to the right, and then float the one with class main_img to the left, but that works partially. 我尝试的是将浮动到右边,然后将类main_img向左浮动,但这部分工作。 There are 3 figure elements in the row, and with float method element with class is at the start of its row, not at the start of the list. 行中有3个图形元素,带有class的float方法元素位于其行的开头,而不是列表的开头。

How can I set this element as first (at the beginning)? 如何将此元素设置为第一个(开头)?

Elements are nodes in a tree. 元素是树中的节点。 What you need is to relocate it to the beginning of its parent. 您需要的是将其重新定位到其父级的开头。

If your element is in a jQuery object referenced by $elem , then with jQuery you'd do this: 如果你的元素在$elem引用的jQuery对象中,那么使用jQuery你会这样做:

$elem.prependTo($elem.parent())

Or this: 或这个:

$elem.parent().prepend($elem)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM