s
o
u
l
s
j
i
e

jQuery_DOM学习之------创建节点及节点属性

DOM创建节点及节点属性

一、创建新的节点并添加到dom中

dom 节点创建的过程(创建节点<元素、属性、文本等>、添加节点的属性、加入到文档中)

jQuery创建元素节点的方法:

创建元素节点:

$("<div></div>");

创建文本节点:

$("<div>直接将文本的内容添加进去</div>");

创建节点并给节点添加属性:
var div = $("<div class='right'><div class='aaron'>动态创建DIV元素节点</div></div>")

此时节点创建完成。需要将节点添加到文档中,添加新内容的四个 jQuery 方法:

  • append() - 在被选元素的结尾插入内容
  • prepend() - 在被选元素的开头插入内容
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
    <style>
    .xin{
        margin-top:15px;
        width:300px;
        height:100px;
        color:#fff;
        background:#f00;
    }
    </style>
</head>

<body>
<h3>创建节点</h3>
<button class="jie">点击我创建节点</button>
    <script type="text/javascript">
$('.jie').on('click',function(){
  $("h3").("<div class='xin'>我是新创建的节点</div>");
});
    </script>
</body>

</html>

 

  • after() - 在被选元素之后插入内容
  • before() - 在被选元素之前插入内容
<script type="text/javascript">
    $("#bt1").on('click', function() {
        //在匹配test1元素集合中的每个元素前面插入p元素
        $(".test1").before('<p style="color:red">before,在匹配元素之前增加</p>', '<p style="color:red">多参数</p>')
    })
    </script>
    <script type="text/javascript">
    $("#bt2").on('click', function() {
        //在匹配test1元素集合中的每个元素后面插入p元素
        $(".test2").after('<p style="color:blue">after,在匹配元素之后增加</p>', '<p style="color:blue">多参数</p>')

    })
    </script>

 

二、DOM内部插入append()与appendTo()

.append()和.appendTo()两种方法功能相同,主要的不同是语法——内容和目标的位置不同

<body>
    <h2>通过append与appendTo添加元素</h2>
    <button id="bt1">点击通过jQuery的append添加元素</button>
    <button id="bt2">点击通过jQuery的appendTo添加元素</button>

    <div class="content"></div>

    <script type="text/javascript">

        $("#bt1").on('click', function() {
    		//.append(), 内容在方法的后面,
    		//参数是将要插入的内容。
    		$(".content").append('<div class="append">通过append方法添加的元素</div>')
    	})

    </script>

    <script type="text/javascript">

    	$("#bt2").on('click', function() {
    		//.appendTo()刚好相反,内容在方法前面,
    		//无论是一个选择器表达式 或创建作为标记上的标记
    		//它都将被插入到目标容器的末尾。
    		$('<div class="appendTo">通过appendTo方法添加的元素</div>').appendTo($(".content"))
    	})

    </script>

</body>

 三、DOM内部插入prepend()与prependTo()

在指定元素之前插入新的节点:

 <script type="text/javascript">
    $("#bt1").on('click', function() {
        //找到class="aaron1"的div节点
        //然后通过prepend在内部的首位置添加一个新的p节点
        $('.aaron1')
            .prepend('<p>prepend增加的p元素</p>')
    })
    </script>
    <script type="text/javascript">
    $("#bt2").on('click', function() {
        //找到class="aaron2"的div节点
        //然后通过prependTo内部的首位置添加一个新的p节点
        $('<p>prependTo增加的p元素</p>')
            .prependTo($('.aaron2'))
    })
    </script>

 四、DOM外部插入insertAfter()与insertBefore()

注意:insertAfter()和insertBefore()都不支持多参数,当有参数时只有第一个参数被执行

<body>
    <h2>通过insertBefore与insertAfter添加元素</h2>
    <button id="bt1">点击通过jQuery的insertBefore添加元素</button>
    <button id="bt2">点击通过jQuery的insertAfter添加元素</button>
    <div class="aaron">
        <p class="test1">测试insertBefore,不支持多参数</p>
    </div>
    <div class="aaron">
        <p class="test2">测试insertAfter,不支持多参数</p>
    </div>
    <script type="text/javascript">
    $("#bt1").on('click', function() {
        //在test1元素前后插入集合中每个匹配的元素
        //不支持多参数
        $('<p style="color:red">测试insertBefore方法增加</p>', '<p style="color:red">多参数</p>').insertBefore($(".test1"))
    })
    </script>
    <script type="text/javascript">
    $("#bt2").on('click', function() {
        //在test2元素前后插入集合中每个匹配的元素
        //不支持多参数
        $('<p style="color:red">测试insertAfter方法增加</p>', '<p style="color:red">多参数</p>').insertAfter($(".test2"))
    })
    </script>
</body>

 四、在DOM中清空节点的内容

.empty()--将选定节点的内容清空

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
  <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>

    <style>
    div {
        background: #ff0;
        width: 300px;
    }
    </style>
</head>

<body>
    <h2>通过empty移除元素</h2>
    <div id="soulsjie">
        <div>这是div的内容</div>
    </div>
    <button>点击通过jQuery的empty移除元素</button>
    <script type="text/javascript">
    $("button").on('click', function() {
        //通过empty移除了当前div元素下的所有p元素
        $("#soulsjie").empty()
    })
    </script> 
</body>
<!--注意:.empty()只是将元素内容清除,被选择的div仍然存在,只是没内容而已-->
</html>

五、remove()---DOM节点删除

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
    <style>
    .test1 {
        background: #bbffaa;
    }
    
    .test2 {
        background: yellow;
    }
    </style>
</head>

<body>
    <h2>remove移除节点</h2>
    <h3>remove无参数方法</h3>
    <button>无参数,删除整个选定的div</button>
    <h3>remove有参数方法</h3>
    <button>有参数,可以删除指定div内符合条件的元素</button>
    <div class="test1">
        <p>测试内容1</p>
        <p>测试内容2</p>
    </div>
     <div class="test2">
        <p>测试内容3</p>
        <p>测试内容4</p>
    </div>
    <script type="text/javascript">
    $("button:first").on('click', function() {
        //删除整个 class=test1的div节点
        $(".test1").remove()
    }) 

    $("button:last").on('click', function() {
        //找到所有p元素中,包含了3的元素
        //这个也是一个过滤器的处理 
        $("p").remove(":contains('4')") 
    })
    </script>
</body>

</html>

.empty()和.remove()区别:

.empty()并不是直接删除DOM节点,只是找到该节点并将该节点的内容清空,而.remove()是直接将DOM节点删除。

六、detach()移除的元素可以通过append()追加到文档,并且该元素的事件和属性不会改变

<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
    <style type="text/css">
    p {
        color:blue;
        cursor:pointer; 
    }
    </style>
</head>

<body>
    <p>绑定点击事件的文本1</p>
    <p>绑定点击事件的文本2</p>
    <h5>被append()恢复的元素会保留被删除之前的属性和事件</h5>
    <button id="bt1">detach()会将元素移除</button>
    <button id="bt2">append()可以将被detach()移除的元素重新添加到dom</button>
    <script type="text/javascript">  
    $('p').click(function() { 
        alert('元素的事件')
    })
    var p; 
    $("#bt1").click(function() {
        if (!$("p").length) return; //去重
        //通过detach方法删除元素
        //只是页面不可见,但是这个节点还是保存在内存中
        //数据与事件都不会丢失
        p = $("p").detach()
    });

    $("#bt2").click(function() {
        //把p元素在添加到页面中
        //事件还是存在
        $("h5").append(p);
    });
    </script>
</body>

</html>

 

 

posted @ 2017-11-06 17:25  soulsjie  阅读(14896)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43