码迷,mamicode.com
首页 > 其他好文 > 详细

Dom终

时间:2016-06-03 12:36:32      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

l创建DOM元素
•createElement(标签名)  创建一个节点
•appendChild(节点)  追加一个节点
–例子:为ul插入li
技术分享
<!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>创建li</title>
<script>
window.onload=function()
{
    //获取按钮
    var oBtn=document.getElementById(‘btn1‘);
    //获取文本
    var oTxt=document.getElementById(‘txt1‘);
    //获取ul
    var oUl=document.getElementById(‘ul1‘);
    oBtn.onclick=function()
    {
        //在do里创建li
        var oLi=document.createElement(‘li‘);
        //文本框的值赋予oli
        oLi.innerHTML=oTxt.value;
        //父类添加子节点
        oUl.appendChild(oLi);
        //清空txt里的值
        oTxt.value=‘‘;
        }
    }
</script>
</head>

<body>

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

 

l插入元素
•insertBefore(节点, 原有节点)  在已有元素前插入
–例子:倒序插入li
技术分享
<!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>父.insertBefore(子节点,谁之前)</title>
<script>
window.onload=function()
{
    var oBtn=document.getElementById(‘btn1‘);
    var oTxt=document.getElementById(‘txt1‘);
    var oUl=document.getElementById(‘ul1‘);
    
    oBtn.onclick=function()
    {    //创建个li
        var oLi=document.createElement(‘li‘);
        //获取ul里的li
        var aLi=oUl.getElementsByTagName(‘li‘);
        //把otxt的值赋予oli
        oLi.innerHTML=oTxt.value;
        //插入到oul里ali[0]坐标之前;
        oUl.insertBefore(oLi, aLi[0]);
        oTxt.value=‘‘;
        }
    }
</script>
</head>
View Code

 

l删除DOM元素
•removeChild(节点)  删除一个节点
–例子:删除li
l文档碎片
•文档碎片可以提高DOM操作性能(理论上)
•文档碎片原理
document.createDocumentFragment
lDOM元素的添加、删除、修改

Dom终

标签:

原文地址:http://www.cnblogs.com/hack-ing/p/5555627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!