Js_dom_02

通过Js创建dom节点 比如发送邮件时的添加附件届时通过添加dom节点完成的

createElement(标签名创建一个节点   appendChild(节点追加一个节点

<!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function ()
{
	var oBtn=document.getElementById('btn1');
	var oTxt=document.getElementById('txt1');
	var oUl=document.getElementById('ul1');
	
	oBtn.onclick=function ()
	{
		var oLi=document.createElement('li');
		
		oLi.innerHTML=oTxt.value;
		
		//父.appendChild(子节点)
		oUl.appendChild(oLi);
	}
}
</script>
</head>

<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="创建Li"/>
<ul id="ul1">
	<li>aaa</li>
</ul>
</body>
</html>


insertBefore 可以指定在某个节点之前插入自己创建的节点 比如发送微博时 新插入的节点显示在第一条

<!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//父.insertBefore(子节点, 谁之前)

window.onload=function ()
{
	var oBtn=document.getElementById('btn1');
	var oTxt=document.getElementById('txt1');
	var oUl=document.getElementById('ul1');
	
	oBtn.onclick=function ()
	{
		var oLi=document.createElement('li');
		var aLi=oUl.getElementsByTagName('li');
		
		oLi.innerHTML=oTxt.value;
		
		if(aLi.length==0)
		{
			oUl.appendChild(oLi);
		}
		else
		{
			oUl.insertBefore(oLi, aLi[0]);
		}
	}
}
</script>
</head>

<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="创建Li"/>
<ul id="ul1">
</ul>
</body>
</html>
removeChild(节点删除一个节点
<!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function ()
{
	var aA=document.getElementsByTagName('a');
	var oUl=document.getElementById('ul1');
	var i=0;
	
	for(i=0;i<aA.length;i++)
	{
		aA[i].onclick=function ()
		{
			oUl.removeChild(this.parentNode);
		}
	}
}
</script>
</head>

<body>
<ul id="ul1">
	<li>sdfsdf <a href="javascript:;">删除</a></li>
	<li>3432 <a href="javascript:;">删除</a></li>
	<li>tttt <a href="javascript:;">删除</a></li>
	<li>ww <a href="javascript:;">删除</a></li>
</ul>
</body>
</html>

文档碎片 一种理论上可以提高性能的方案但是实际上并没有 
文档碎片的原理 : 
比如要添加1W个节点到ul节点下正常的做法是createElement 然后 appendChild 如此循环1W次 但是
文档碎片的做法是createDocumentFragment
创建出来的1W个节点先存起来然后 一次性appendChild
比较以下两个案例
普通的添加
<!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function ()
{
	var oBtn=document.getElementById('btn1');
	var oUl=document.getElementById('ul1');
	
	oBtn.onclick=function ()
	{
		var iStart=new Date().getTime();
		var i=0;
		
		for(i=0;i<100000;i++)
		{
			var oLi=document.createElement('li');
			
			oUl.appendChild(oLi);
		}
		
		alert(new Date().getTime()-iStart);
	}
}
</script>
</head>

<body>
<input id="btn1" type="button" value="普通"/>
<ul id="ul1">
</ul>
</body>
</html>
文档碎片的方式添加
<!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>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function ()
{
	var oBtn=document.getElementById('btn1');
	var oUl=document.getElementById('ul1');
	
	oBtn.onclick=function ()
	{
		var iStart=new Date().getTime();
		var oFrag=document.createDocumentFragment();
		var i=0;
		
		for(i=0;i<100000;i++)
		{
			var oLi=document.createElement('li');
			
			oFrag.appendChild(oLi);
		}
		
		oUl.appendChild(oFrag);
		
		alert(new Date().getTime()-iStart);
	}
}
</script>
</head>

<body>
<input id="btn1" type="button" value="碎片"/>
<ul id="ul1">
</ul>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值