如何设置DOM元素作为第一个孩子? [英] How to set DOM element as the first child?
本文介绍了如何设置DOM元素作为第一个孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
长时间的方式是迭代E的孩子,并移动'键key ++,但我确信有一个更漂亮的方式。
解决方案
var eElement; // some E DOM instance
var newFirstElement; //元素应该首先在E
eElement.insertBefore(newFirstElement,eElement.firstChild);
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.
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屋!
查看全文