You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const ul = document.querySelector('ul');
const first = ul.firstChild;
const third = first.nextElementSibling.nextElementSibling;
ul.insertBefore(third, first);
不使用JS,只使用CSS实现:
ul li:first-child {
order: 3;
}
ul li:nth-child(3) {
order: 1;
}
使用JS实现:
const ul = document.querySelector('ul');
const first = ul.firstChild;
const third = first.nextElementSibling.nextElementSibling;
ul.insertBefore(third, first);
不使用JS,只使用CSS实现:
ul li:first-child {
order: 3;
}
ul li:nth-child(3) {
order: 1;
}
需要注意的是,这种方式只会改变元素在视觉上的位置,不会对DOM结构产生任何影响。另外,order属性只在flex布局下生效。``
—
Reply to this email directly, view it on GitHub
<#55 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGVHFFKZE2FVEOABEFK4QX3XDNTZZANCNFSM5TFAPAJQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
Activity
NoBey commentedon Apr 12, 2022
Y-J-H commentedon Apr 12, 2022
jy910222 commentedon Apr 12, 2022
js方法insertBefore 只改变位置,不新增节点
css
注:JS实现中,请考虑头、尾两个节点的边界情况,这时候适用
appendChildlinrunzheng commentedon Apr 14, 2022
yvonneit commentedon Mar 31, 2023
使用JS实现:
insertBefore() 是DOM API中的一个方法,用于将一个节点插入到指定节点之前。它接受两个参数:要插入的节点和参照节点。
如果参照节点是 null,则相当于 appendChild() 方法,将节点插入到父节点的最后一项。需要注意的是,一旦在 DOM 中移动了一个节点,它的所有事件监听器和数据都将被保留。
不使用JS,只使用CSS实现:
order 是 CSS 中 flex 布局的一个属性,用于指定弹性子元素的排列顺序。它可以接受一个整数值,默认为 0。
order 的工作原理是将所有弹性子元素按照它们的 order 属性进行排序(从小到大)。如果两个元素的 order 属性相等,则按照它们在文档流中出现的顺序进行排序。需要注意的是,order 只改变元素的显示顺序,不影响 DOM 结构。
imondo commentedon Mar 31, 2023
jacksmas commentedon Apr 28, 2023
使用JS实现:
不使用JS,只使用CSS实现:
需要注意的是,这种方式只会改变元素在视觉上的位置,不会对DOM结构产生任何影响。另外,
order属性只在flex布局下生效。``xingbaoliang commentedon May 26, 2023
imondo commentedon May 26, 2023