jQuery - 节点的插入

动态创建的元素是不够的,它只是临时存放在内存中,最终我们需要放到页面文档并呈现出来。

内部插入append()与appendTo()

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
    <style>
    .content {
        width: 300px;
    }
    .append{
        background-color: blue;
    }
    .appendTo{
        background-color: red;
    }
    </style>
</head>

<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>

</html>

外部插入after()与before()

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
    <style>.aaron{border: 1px solid red;}</style>
</head>
<body>
    <h2>通过before与after添加元素</h2>
    <button id="bt1">点击通过jQuery的before添加元素</button>
    <button id="bt2">点击通过jQuery的after添加元素</button>
    
    <div class="aaron">
        <p class="test1">测试before</p>
    </div>
    <div class="aaron">
        <p class="test2">测试after</p>
    </div>

    <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>
</body>

内部插入prepend()与prependTo()

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
    <style>
        .aaron1{border: 1px solid red;}
        .aaron1 p {color: red;}
        .aaron2{border: 1px solid blue;}
        .aaron2 p {color: blue;}
    </style>
</head>

<body>
    <h2>通过prepend与prependTo添加元素</h2>
    <button id="bt1">点击通过jQuery的prepend添加元素</button>
    <button id="bt2">点击通过jQuery的prependTo添加元素</button>
    <div class="aaron1">
        <p>测试prepend</p>
    </div>
    <br/>
    <div class="aaron2">
        <p>测试prependTo</p>
    </div>

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

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

</html>

外部插入insertAfter()与insertBefore()

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
    <style>.test1 {background: #bbffaa;}.test2 {background: yellow;}</style>
</head>
<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>
</html>

猜你喜欢

转载自blog.csdn.net/cai181191/article/details/81077397