DOM实现了增删改和分页

 

今天用DOM实现了增删改和分页在这里与大家分享由于时间问题仿百度分页还没有实现请大家等待。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>addUser.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">


		<script type="text/javascript" src="./js/wpCalendar.js">
</script>

	</head>

	<body>
		<div align="center">
			<h1>
				显示有的用户界面
			</h1>

			<div
				style="border: 1px red solid; margin-bottom: 100px; padding: 10px 10%">

				<table border="1px" cellpadding="0" cellspacing="0" id="tusers">
					<thead>
						<tr>
							<th>
								<input type="checkbox" name="chbk" id="chbk1"
									οnclick="selectAll()" />
							</th>
							<th>
								名称
							</th>
							<th>
								性别
							</th>
							<th>
								邮箱
							</th>
							<th>
								出生日期
							</th>
							<th>
								操作
							</th>
						</tr>
					</thead>

					<tbody id="users">

					</tbody>

				</table>

				<div id="pages">

				</div>
			</div>

			<div style="border: 1px blue solid;">
				<form>
					<table id="divs">
						<tbody id="addUsers">
							<tr>
								<td>
									用户名:
								</td>
								<td>
									<input type="text" name="name" id="name" />
								</td>
							</tr>
							<tr>
								<td>
									性别:
								</td>
								<td>
									<select id="sex">
										<option value="男">
											男
										</option>
										<option value="女">
											女
										</option>
									</select>
								</td>
							</tr>

							<tr>
								<td>
									邮箱:
								</td>
								<td>
									<input type="text" name="email" id="email" />
								</td>
							</tr>
							<tr>
								<td>
									出生日期:
								</td>
								<td>
									<input type="text" id="bir" name="bir" />
									<input type=button value="点击看我"
										οnclick=
	showCalendar(this, document.all.bir);
/>
								</td>
							</tr>

							<tr id="addu">
								<td colspan="2">
									<input type="button" value="添加" οnclick=
	addUser();
id="add" />
								</td>
							</tr>

							<tr id="addu1">
								<td colspan="2">
									<input type="button" value="修改" id="upduser" />
								</td>
							</tr>
						</tbody>
					</table>
				</form>
			</div>
		</div>
	</body>
</html>


<script type="text/javascript">
	window.onload = function() {
		document.getElementById("addu1").style.display = "none";

	}

	function addUser() {
		var name = document.getElementById("name").value;
		var sex = document.getElementById("sex").value;
		var email = document.getElementById("email").value;
		var bir = document.getElementById("bir").value;

		//获取表格节点对象
		var tusers = document.getElementById("tusers");

		//创建行
		var tr1 = document.createElement("tr");
		var cbk = document.createElement("td");
		var tname = document.createElement("td");
		var tsex = document.createElement("td");
		var temail = document.createElement("td");
		var tbir = document.createElement("td");
		var toper = document.createElement("td");

		var cbk1 = document.createElement("input");
		cbk1.setAttribute("type", "checkbox");
		cbk1.setAttribute("name", "chbk");

		cbk.appendChild(cbk1);
		tname.appendChild(document.createTextNode(name));
		tsex.appendChild(document.createTextNode(sex));
		temail.appendChild(document.createTextNode(email));
		tbir.appendChild(document.createTextNode(bir));
		var adelete = document.createElement("a");
		var aupdate = document.createElement("a");

		adelete.setAttribute("href", "#");
		aupdate.setAttribute("href", "#");

		adelete.appendChild(document.createTextNode("删除 |"));
		aupdate.appendChild(document.createTextNode("修改"));
		toper.appendChild(adelete);
		toper.appendChild(aupdate)

		//往行中添加
		tr1.appendChild(cbk);
		tr1.appendChild(tname);
		tr1.appendChild(tsex);
		tr1.appendChild(temail);
		tr1.appendChild(tbir);
		tr1.appendChild(toper);

		var users = document.getElementById("users");
		users.appendChild(tr1);
		tusers.appendChild(users);

		//删除操作
		adelete.onclick = function() {
			users.removeChild(adelete.parentNode.parentNode);
			testPage();
		}

		//修改操作
		aupdate.onclick = function() {
			document.getElementById("addu").style.display = "none";
			document.getElementById("addu1").style.display = "block";

			var utr = aupdate.parentNode.parentNode;
			var utrs = utr.childNodes;

			document.getElementById("name").value = utrs[1].innerHTML;
			document.getElementById("sex").value = utrs[2].innerHTML;
			document.getElementById("email").value = utrs[3].innerHTML;
			document.getElementById("bir").value = utrs[4].innerHTML;

			var upUser = document.getElementById("upduser");

			upUser.onclick = function() {
				utr.childNodes[1].innerHTML = document.getElementById("name").value;
				utr.childNodes[2].innerHTML = document.getElementById("sex").value;
				utr.childNodes[3].innerHTML = document.getElementById("email").value
				utr.childNodes[4].innerHTML = document.getElementById("bir").value
				document.getElementById("addu1").style.display = "none";
				document.getElementById("addu").style.display = "block";
			}

		}

		//当添加用户的时候调用分页技术
		testPage();
	}
	//定义分页标签节点对象并创建
	var indexPage = document.createElement("a");
	var upPage = document.createElement("a");
	var downPage = document.createElement("a");
	var endPage = document.createElement("a");

	var pages = document.getElementById("pages");
	var nowpage = 1;

	//分页技术的实现
	function testPage() {
		var tbodyUsers = document.getElementById("users");
		var trUsers = tbodyUsers.getElementsByTagName("tr");
		//获取总记录数
		var countRecord = trUsers.length;
		//每页显示的记录数
		var PAGESIZE = 2;
		//总页数
		var countPage = (countRecord % PAGESIZE == 0 ? countRecord / PAGESIZE
				: Math.ceil(countRecord / PAGESIZE));
		//当前页信息
		// =======>从那条记录开始 (nowpage-1)*PAGESIZE;
		//var nowpage = countPage;

		//获取创建分页标签的节点对象
		var pages = document.getElementById("pages");
		//如果没有创建分页标签的节点则创建节点对象
		if (!pages.hasChildNodes()) {
			getPages(nowpage);
		}
		if (countRecord % PAGESIZE != 0) {
					var dop = document.getElementById("dop");
			var somePage = document.createElement("a");
			somePage.appendChild(document.createTextNode("[" + countPage + "]"));
			somePage.setAttribute("href", "#");
			pages.insertBefore(somePage, dop);
			somePage.onclick = function() {
				var SHOWPAGE = 6;
				if (countPage < (SHOWPAGE / 2 + 1)) {
					startpage = 1;
					endpage =countPage;
				} else {
					if (nowpage <= (SHOWPAGE / 2 + 1)) {
						startpage = 1;
						endpage = nowpage + 2;
						if (endpage >= countPage) {
							endpage = countPage;
						}
					} else {
						startpage = nowpage - 3;
						endpage = nowpage + 2;
						if (endpage >= countPage) {
							endpage = countPage;
							if (countPage < SHOWPAGE) {
								startpage = 1;
							} else {
								startpage = endpage - 5;
							}
						}
					}
				}
			}
			PageInfo(startindex, endindex, countRecord, trUsers)
		}

		//当点击首页时的操作
		indexPage.onclick = function() {
			nowpage = 1;
			//显示首页的记录
			indexPageInfo(countRecord, trUsers, nowpage, PAGESIZE);

		}
		//当点击上一页的操作
		upPage.onclick = function() {
			if (nowpage - 1 > 1) {
				nowpage -= 1;
			} else {
				nowpage = 1;
				indexPageInfo(countRecord, trUsers);
			}

			//显示上一页记录
			var startindex = (nowpage - 1) * PAGESIZE;

			var endindex = startindex + PAGESIZE;

			PageInfo(startindex, endindex, countRecord, trUsers)
		}
		//当点击下一页的操作
		downPage.onclick = function() {

			if (nowpage + 1 >= countPage) {

				nowpage = countPage;

			} else {

				nowpage += 1;
			}

			//显示上一页记录
			var startindex = (nowpage - 1) * PAGESIZE;

			var endindex = startindex + PAGESIZE;

			PageInfo(startindex, endindex, countRecord, trUsers)
		}
		//当点击最后一页的操作
		endPage.onclick = function() {

			nowpage = countPage;

			if (nowpage > 1) {

				var startindex = (nowpage - 1) * PAGESIZE;

				for ( var i = 0; i < countRecord; i++) {

					if (i < startindex) {

						trUsers[i].style.display = "none";

					} else {
					
						trUsers[i].style.display = "block";
						
					}

				}
			} else {

				indexPageInfo(countRecord, trUsers, nowpage, PAGESIZE);

			}

		}
	}
	//首页的显示记录
	function indexPageInfo(countRecord, trUsers, nowpage, PAGESIZE) {

		if (nowpage == 1) {

			for ( var i = 0; i <= PAGESIZE; i++) {

				trUsers[i].style.display = "block";
			}
		}
		if (countRecord <= PAGESIZE) {

		} else {

			for ( var i = 2; i < countRecord; i++) {

				trUsers[i].style.display = "none";
			}
		}
	}
	//上一页 下一页的显示记录
	function PageInfo(startindex, endindex, countRecord, trUsers) {

		for ( var i = 0; i < countRecord; i++) {

			if (i >= startindex && i < endindex) {

				trUsers[i].style.display = "block";

			} else {

				trUsers[i].style.display = "none";
			}
		}

	}

	//最后一页的显示记录

	//创建分页的相应链接地址
	function getPages(numpage) {

		indexPage.appendChild(document.createTextNode("首页"));
		indexPage.setAttribute("href", "#");

		upPage.appendChild(document.createTextNode("上一页"));
		upPage.setAttribute("href", "#");

		downPage.appendChild(document.createTextNode("下一页"));
		downPage.setAttribute("href", "#");
		downPage.setAttribute("id", "dop");

		endPage.appendChild(document.createTextNode("末页"));
		endPage.setAttribute("href", "#");

		//获取创建连接的位置的节点对象

		//添加到节点中
		pages.appendChild(indexPage);
		pages.appendChild(upPage);
		pages.appendChild(downPage);
		pages.appendChild(endPage);

	}
	function selectAll() {
		//获取所有用户信息的根节点
		var users = document.getElementById("users");
		//获取根节点中的input标签节点
		var ips = users.getElementsByTagName("input");
		//获取chbk的节点对象
		var chbk = document.getElementById("chbk1");

		for ( var i = 0; i < ips.length; i++) {
			//让遍历的结果于chbk中接到的checked属性值一致即可
			ips[i].setAttribute("checked", chbk.getAttribute("checked"));
		}

	}

</script>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值