JavaScript DOM节点InsertBefore()方法
- 节点对象用于在HTML文档中表示和添加节点。
- InsertBefore()方法用于将新子节点插入到现有节点上,也可用于插入或者移动到现有节点或者元素。
语法
node.insertBefore(newnode,existingnode)
JavaScript DOM节点InsertBefore()方法示例
<!DOCTYPE html>
<html>
<body style ="background-color:yellow">
<ul id="myList"><li>c#</li><li>java</li></ul>
<p>插入新内容到列表中</p>
<button onclick="myFunction()">INSERT</button>
<script type="text/javascript">
function myFunction() {
var newItem = document.createElement("LI")
var textnode = document.createTextNode("python")
newItem.appendChild(textnode)
var list = document.getElementById("myList")
list.insertBefore(newItem, list.childNodes[0]);
}
</script>
</body>
</html>
日期:2020-04-18 01:09:23 来源:oir作者:oir
