【功能说明】
利用replaceChild制作简单的吞噬效果
【HTML代码说明】
<ul id="list"> <li >1</li> <li >2</li> <li >3</li> <li >4</li> <li >5</li> <li >6</li> </ul>
【CSS代码说明】
.in{ height: 20px; line-height: 20px; width: 20px; background-color: blue; text-align: center; color: white;}
【JS代码说明】
<script>var oList = document.getElementById('list');//新增一个li元素var oAdd = document.createElement('li');//设置新增元素的css样式oAdd.className = "in";oAdd.style.cssText = 'background-color:red;border-radius:50%';//1s后oAdd替换第0个lisetTimeout(function(){ oList.replaceChild(oAdd,document.getElementsByTagName('li')[0]); //1s后执行incrementNumber函数 setTimeout(incrementNumber,1000); },1000);function incrementNumber(){ //获取oList中第1个li var oLi1 = document.getElementsByTagName('li')[1]; //若存在则进行替换处理 if(oLi1){ oList.replaceChild(oAdd,oLi1); setTimeout(incrementNumber,1000); }}</script>
【效果展示】
【源码查看】
原标题:利用replaceChild制作简单的吞噬效果【jsDEMO】
关键词:JS