Jquery | 外部插入节点

                                                         

after(content) :

//在 span 元素外部的后面 插入 "<span><b>Write Less Do More</b><span>"
 $("span").after("<span><b>Write Less Do More</b><span>"); // 插入内容

 

after(function):

    //在 span 元素外部的后面插入 reHtml方法返回的内容 
    $("span").after(retHtml); // 插入内容
    function retHtml() {
        var str = "<span><b>Write Less Do More</b><span>";
        return str;
    }

 

before(content)

//在 span 元素外部的前面 插入 "<span><b>Write Less Do More</b><span>"
$("span").before("<span><b>Write Less Do More</b><span>"); // 插入内容

 

before(funtion)

//在 span 元素外部的前面插入 reHtml方法返回的内容
$("span").before(retHtml); // 插入内容
function retHtml() {
var str = "<span><b>Write Less Do More</b><span>";
return str;
}

 

insertAfter(content)

// 将"<span><b>Write Less Do More</b><span>"插入 span 元素外部的后面
$("<span><b>Write Less Do More</b><span>").insertAfter($("span")); // 插入内容

 

insertBefore(content)

// 将"<span><b>Write Less Do More</b><span>"插入 span 元素外部的前面
 $("<span><b>Write Less Do More</b><span>").insertBefore($("span")); // 插入内容

 

 

<!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>
    <title> 动态插入节点方法 </title>
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    <style type="text/css">
        body {
            font-size: 13px
        }
    </style>
    <script type="text/javascript">
        $(function () {

            $("#bt0").click(function () {

            })

            $("#bt1").click(function () {
                $("span").after(retHtml); // 插入内容

            })

            $("#bt2").click(function () {
                $("span").before(retHtml); // 插入内容
            })

            $("#bt3").click(function () {
                $("<span><b>Write Less Do More</b><span>").insertAfter($("span")); // 插入内容
                
            })

            $("#bt4").click(function () {
                $("<span><b>Write Less Do More</b><span>").insertBefore($("span")); // 插入内容
                
            })

            function retHtml() {
                var str = "<span><b>Write Less Do More</b><span>";
                return str;
            }
            var htmll = $('body').html();
            $('p').text(htmll);

        })
    </script>
</head>

<body>
    <span>jQuery</span>
    <p></p>

    <button id="bt1">after</button>
    <button id="bt2">before</button>
    <button id="bt3">insertAfter</button>
    <button id="bt4">insertBefore</button>
</body>

</html>

 

转载于:https://www.cnblogs.com/jj81/p/9796133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值