码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript操作DOM元素

时间:2016-08-31 07:14:19      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:javascript


1、动态创建DOM的步骤

(1)调用document的createElement方法来创建具有指定标签的DOM对象

(2)然后通过调用某个元素的appendChild();方法将新创建的元素添加到相应的元素下。


2、操作DOM的方法

createElement(‘element‘);创建一个节点

document.createElement(‘<input type="button" value="hello" />‘);快速创建元素,并且赋值,但是注意设置的inner部分不会被设置
document.createElement(‘<a href="www.baidu.com">百度</a>‘);注意:"百度"这两个字写不进去

appendChild(node);追加一个节点

removeChild(node);移除一个节点

replaceChild(new,old);替换一个节点

insertBefore(new,参照);把节点加到前面(插到某个节点前面)

firstChild

lastChild



3、示例代码

示例代码:添加一个div元素

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        onload=function(){
            document.getElementById(‘btn‘).onclick=function(){
                var divObj = document.createElement(‘div‘);
                document.body.appendChild(divObj);
            };
        };
    </script>
</head>
<body>
    <input type="button" id="btn" value="创建div"/>
</body>
</html>

效果图

技术分享


示例代码:添加一个div元素(带样式)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        onload=function(){
            document.getElementById(‘btn‘).onclick=function(){
                var divObj = document.createElement(‘div‘);
                divObj.style.width = "300px";
                divObj.style.height="180px";
                divObj.style.border="solid 1px red";
                divObj.style.margin = "5px";
                document.body.appendChild(divObj);
            };
        };
    </script>
</head>
<body>
    <input type="button" id="btn" value="创建div"/>
</body>
</html>

效果图

技术分享


示例代码:在div元素中添加按钮和文本框

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        var myOrder = 0;
        onload = function(){
            document.getElementById(‘btnCreateDiv‘).onclick=function(){
                if(!document.getElementById(‘dv‘))
                {
                    var divObj = document.createElement("div");
                    divObj.id = "dv";
                    divObj.style.width = "300px";
                    divObj.style.height = "180px";
                    divObj.style.border = "solid 1px red";
                    divObj.style.margin = "2px";

                    document.body.appendChild(divObj);
                }
            };

            document.getElementById(‘btnCreateInput‘).onclick= function () {
                var divObj = document.getElementById(‘dv‘);
                if(divObj)
                {
                    myOrder++;
                    var inputObj = document.createElement("input");
                    inputObj.type = "button";
                    inputObj.value = "按钮" + myOrder;
                    divObj.appendChild(inputObj);
                }
            };

            document.getElementById(‘btnCreateTxt‘).onclick = function(){
                var divObj = document.getElementById(‘dv‘);
                if(divObj){
                    myOrder++;
                    var inputObj = document.createElement("input");
                    inputObj.type = "text";
                    inputObj.value = "文本框"+myOrder;
                    divObj.insertBefore(inputObj,divObj.firstChild);
                }
            };
        };
    </script>
</head>
<body>
    <input type="button" id="btnCreateDiv" value="创建层"/>
    <input type="button" id="btnCreateInput" value="创建按钮"/>
    <input type="button" id="btnCreateTxt" value="创建文本框"/>
</body>
</html>


效果图一(firefox下第二个按钮不能用,不知道是为什么)

技术分享

效果图二

技术分享



示例代码:删除div中所有元素

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        var myOrder = 0;
        onload = function(){
            document.getElementById(‘btnCreateDiv‘).onclick=function(){
                if(!document.getElementById(‘dv‘))
                {
                    var divObj = document.createElement("div");
                    divObj.id = "dv";
                    divObj.style.width = "300px";
                    divObj.style.height = "180px";
                    divObj.style.border = "solid 1px red";
                    divObj.style.margin = "2px";

                    document.body.appendChild(divObj);
                }
            };

            document.getElementById(‘btnCreateInput‘).onclick= function () {
                var divObj = document.getElementById(‘dv‘);
                if(divObj)
                {
                    myOrder++;
                    var inputObj = document.createElement("input");
                    inputObj.type = "button";
                    inputObj.value = "按钮" + myOrder;
                    divObj.appendChild(inputObj);
                }
            };

            document.getElementById(‘btnCreateTxt‘).onclick = function(){
                var divObj = document.getElementById(‘dv‘);
                if(divObj){
                    myOrder++;
                    var inputObj = document.createElement("input");
                    inputObj.type = "text";
                    inputObj.value = "文本框"+myOrder;
                    divObj.insertBefore(inputObj,divObj.firstChild);
                }
            };

            document.getElementById(‘btnDelete‘).onclick = function(){
                var divObj = document.getElementById(‘dv‘);
                if(divObj){
                    while(divObj.firstChild)
                    {
                        divObj.removeChild(divObj.firstChild);
                    }
                }
            };
        };
    </script>
</head>
<body>
    <input type="button" id="btnCreateDiv" value="创建层"/>
    <input type="button" id="btnCreateInput" value="创建按钮"/>
    <input type="button" id="btnCreateTxt" value="创建文本框"/>
    <input type="button" id="btnDelete" value="删除div中所有元素"/>
</body>
</html>

效果图

技术分享






Javascript操作DOM元素

标签:javascript

原文地址:http://lsieun.blog.51cto.com/9210464/1844569

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