如何将 DOM 元素设置为第一个子元素? [英] How to set DOM element as the first child?
本文介绍了如何将 DOM 元素设置为第一个子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有元素 E,我正在向它附加一些元素.突然,我发现下一个元素应该是E的第一个孩子.有什么技巧,怎么做?方法 unshift 不起作用,因为 E 是一个对象,而不是数组.
I have element E and I'm appending some elements to it. All of a sudden, I find out that the next element should be the first child of E. What's the trick, how to do it? Method unshift doesn't work because E is an object, not array.
很长的路要走的是遍历 E 的孩子们并移动他们的 key++,但我相信有一种更漂亮的方法.
Long way would be to iterate trough E's children and to move'em key++, but I'm sure that there is a prettier way.
推荐答案
var eElement; // some E DOM instance
var newFirstElement; //element which should be first in E
eElement.insertBefore(newFirstElement, eElement.firstChild);
这篇关于如何将 DOM 元素设置为第一个子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文