动态添加DOM

本文最后更新于:2023年3月30日 下午

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload = function(){
var pt = document.getElementById('pt');
var btn = document.getElementById('btn');
var ul = document.getElementById('ul');
btn.onclick = function() {
var newli = document.createElement('li');
var ali = ul.getElementsByTagName('li');
newli.innerHTML = pt.value;
//ul.appendChild(newli);// 插入元素到后面
/*if (ali.length === 0) {
ul.appendChild(newli);
} else {
ul.insertBefore(newli, ali[0]);
}*/
// 插入到元素的前面.后面ajax服务器获取数据很重要的应用
ali.length === 0 ? ul.appendChild(newli) : ul.insertBefore(newli, ali[0]);
}
}
</script>
</head>
<body>
<input type="text" id="pt" />
<input type="button" value="添加" id="btn" />
<ul id="ul"></ul>
</body>
</html>

动态添加DOM
https://seven3.site/js/动态添加DOM/
作者
Seven3s
发布于
2015年1月23日
许可协议