如何设置DOM元素作为第一个孩子? [英] How to set DOM element as the first child?

查看:283
本文介绍了如何设置DOM元素作为第一个孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有元素E,我附加了一些元素。突然之间,我发现下一个元素应该是E的第一个孩子。诀窍是怎么做的?方法unshift不起作用,因为E是一个对象,而不是数组。



长时间的方式是迭代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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆