将 DIV 元素移动到父元素的底部(作为最后一个子元素)

发布于 2024-09-13 12:15:52 字数 588 浏览 2 评论 0原文

我正在尝试为我正在开发的网站的主页顶部创建一个不断旋转的横幅。我能想到的保持它不断旋转的最佳方法是获取第一个 DIV (firstChild),并在它滑出视图后将其移动到堆栈的末尾。

这:

<div id='foo0'></div>
<div id='foo1'></div>
<div id='foo2'></div>
<div id='foo3'></div>

应该变成这样:

<div id='foo1'></div>
<div id='foo2'></div>
<div id='foo3'></div>
<div id='foo0'></div>

我使用 Prototype 框架...并且我尝试通过使用我自己的方法克隆元素并将其插入到父 DIV 的底部来做到这一点,但我发现并非所有样式属性正在被继承,我想放弃这个方法,因为我不希望移动的内容是元素的副本/克隆,而是实际的元素本身。

谢谢。

I'm trying to create a continually rotating banner for the top of the homepage of a site I'm developing. The best way that I can figure to keep it constantly in rotation is to take the first DIV (firstChild) and move it to the end of the stack once it's slid out of view.

This:

<div id='foo0'></div>
<div id='foo1'></div>
<div id='foo2'></div>
<div id='foo3'></div>

Should become this:

<div id='foo1'></div>
<div id='foo2'></div>
<div id='foo3'></div>
<div id='foo0'></div>

I use the Prototype framework... and I've tried to do this by cloning the element using my own method and inserting it into the bottom of the parent DIV, but I find that not all of the style attributes are being carried over, and I'd like to abandon this method because I don't want what's being moved to be a copy/clone of the element, but the actual element itself.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-09-20 12:15:52

假设 div 有一个有效的父节点,这里有一些简单的 javascript 来完成此操作:

var d = document.getElementById('foo0');
d.parentNode.appendChild(d);

本质上,您获取节点,然后将其附加到其父节点。 appendChild,如果节点已经是 DOM 的一部分,则会将节点从当前位置移动到 DOM 中的新位置。

Here's some plain javascript to do it, assuming the div has a valid parent:

var d = document.getElementById('foo0');
d.parentNode.appendChild(d);

Essentially you get the node, then append it to its parent. appendChild, if the node is already part of the DOM, will move the node from its current position to the new position in the DOM.

我很坚强 2024-09-20 12:15:52

用父 div 包裹你的 div:

    var parentElement =  document.querySelector("#yourParentDiv");
    parentElement.appendChild(parentElement.firstElementChild);

wrap your divs with a parent div:

    var parentElement =  document.querySelector("#yourParentDiv");
    parentElement.appendChild(parentElement.firstElementChild);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文