我可以使用.appendChild()将元素插入到元素的开头吗? [英] Can I insert elements to the beginning of an element using .appendChild()?

查看:299
本文介绍了我可以使用.appendChild()将元素插入到元素的开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户在下拉框中选择一个选项,则必须添加一个标签和一个文本框.使用appendChild,这些元素将添加到container的末尾.

If the user selects an option in a dropdown box, there must be added a label and a textbox. Using appendChild, these elements get added to the end of the container.

var newFreeformLabel = document.createElement('label');
newFreeformLabel.innerHTML = 'Omschrijving:';

var newFreeformField = document.createElement('input');
newFreeformField.className = 'textfield';
newFreeformField.name = 'factuur_orderregel[]';

var newFreeformSpacer = document.createElement('div');
newFreeformSpacer.className = 'spacer';

container.appendChild(newFreeformLabel);
container.appendChild(newFreeformField);
container.appendChild(newFreeformSpacer);

问题在于这些元素应插入container的开头,而不是结尾.

The issue is that these elements should be inserted at the beginning of container, not at the end.

PrototypeJS是否有解决方案?

Is there a solution to this in PrototypeJS?

推荐答案

appendChild一样,DOM节点也具有

As well as appendChild, DOM nodes have an insertBefore method

container.insertBefore(newFreeformLabel, container.firstChild);

这篇关于我可以使用.appendChild()将元素插入到元素的开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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