jQuery node append

Node append


##Father-child relationship append

  • append(content): Append content to each matching element

  • prepend(content): Append content to the inside of each matching element

  • appendTo(content): Append all matching elements after Append to another, specified element collection

  • prependTo(content): Prepend all matching elements to another, specified set The examples of

in the element collection are as follows:

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function  f1(){
            //主动追加
            //新节点追加
            //append() 后置 
            $("#yi").append("<li>成都市</li>");
            //prepend() 前置 
            $("#yi").prepend("<li>杭州市</li>");

            //已有节点追加(节点发生物理位置移动)
            $("#yi").append($("#gai"));
        }
        function f2(){
            //被动追加
            //appendTo  后置追加
            //prependTo  前置追加
            //新节点追加
            $("<li>武汉市</li>").appendTo('#yi');
            $("<li>天津市</li>").prependTo('#yi');
            //$("html标签")---创建节点--->createElement等方法
            //已有节点追加
            $('#er li:eq(1)').appendTo('#yi');
        }
        </script>
        <style type="text/css">
        div {width:300px; height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <ul id="yi">
            <li>北京</li>
            <li>上海</li>
            <li>广州</li>
        </ul>
        <ul id="er">
            <li>福州市</li>
            <li>合肥市</li>
            <li id="gai">郑州市</li>
        </ul>
        <input type="button" value="追加1" onclick="f1()" />
        <input type="button" value="追加2" onclick="f2()" />
    </body>
</html>


Brother relationship append

  • after(content): Insert content after each matching element

  • before(content): Insert content before each matching element

  • insertAfter(content): Insert all matching elements after another, specified set of elements

  • insertBefore(content): Insert all matching elements in front of another, specified element collection

The example is as follows:

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function  f1(){
            //主动追加
            //after()  后置追加
            //before()  前置追加
            //新节点
            $('#yi li:last').after("<li>深圳</li>");
            $('#yi li:first').after("<li>成都市</li>");
            $('#fei').before("<li>杭州市</li>");
            //已有节点追加
            $('#fei').after($('#gai'));
        }
        </script>
        <style type="text/css">
        div {width:300px; height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <ul id="yi">
            <li>北京</li>
            <li id="fei">上海</li>
            <li>广州</li>
        </ul>
        <ul id="er">
            <li>福州市</li>
            <li>合肥市</li>
            <li id="gai">郑州市</li>
        </ul>
        <input type="button" value="追加1" onclick="f1()" />
    </body>
</html>


Continuing Learning
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
<title>php.cn</title>
<meta charset="utf-8" />
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script>
function f1(){
//
//
//append()
$("#yi").append("<li></li>");
//prepend()
$("#yi").prepend("<li></li>");
//()
$("#yi").append($("#gai"));
}
function f2(){
//
//appendTo
//prependTo
//
$("<li></li>").appendTo('#yi');
$("<li></li>").prependTo('#yi');
//$("html")------>createElement
//
$('#er li:eq(1)').appendTo('#yi');
}
</script>
<style type="text/css">
div {width:300px; height:200px; background-color:pink;}
</style>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
submitReset Code
图片放大关闭