js中常用追加元素的幾種方法


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js中常用追加元素的幾種方法</title>
<link rel="stylesheet" href="css/rest.css" />
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<style>
.container {
    width: 1200px;
    padding: 10px;
    margin: 50px auto;
    border: 1px solid lightcoral;
}

#wrap {
    border: 1px solid lightseagreen;
}

.container p {
    height: 30px;
    line-height: 30px;
}

.btn-group {
    margin-top: 20px;
}

button {
    width: 80px;
    height: 32px;
    margin-right: 10px;
    line-height: 32px;
    text-align: center;
    border: 0px;
}
</style>
</head>
<body>
    <div class="container">
        <div id="wrap">
            <p class="first">我是第一個子元素</p>
            <p class="second">我是第二個子元素</p>
        </div>
        <div class="btn-group">
            <button class="append">append</button>
            <button class="appendTo">appendTo</button>
            <button class="prepend">prepend</button>
            <button class="prependTo">prependTo</button>
            <button class="after">after</button>
            <button class="before">before</button>
            <button class="appendChild" οnclick="appChild()">appendChild</button>
            <button class="insertAfter">insertAfter</button>
            <button class="insertBefore">insertBefore</button>
        </div>
    </div>
</body>
</html>
<script src="js/jquery-1.9.1.min.js"></script>
<script>    
$(function(){        
    //append(),在父級最后追加一個子元素        
    $(".append").click(function(){
        $("#wrap").append("<p class='three'>我是子元素append</p>");        });                
    //appendTo(),將子元素追加到父級的最后        
    $(".appendTo").click(function(){            
        $("<p class='three'>我是子元素appendTo</p>").appendTo($("#wrap"));        });                
    //prepend(),在父級最前面追加一個子元素        
    $(".prepend").click(function(){            
        $("#wrap").prepend("<p class='three'>我是子元素prepend</p>");        });                
    //prependTo(),將子元素追加到父級的最前面        
    $(".prependTo").click(function(){            
        $("<p class='three'>我是子元素prependTo</p>").prependTo($("#wrap"));        });                
    //after(),在當前元素之后追加(是同級關系)        
    $(".after").click(function(){            
        $("#wrap").after("<p class='siblings'>我是同級元素after</p>");        });                
    //before(),在當前元素之前追加(是同級關系)        
    $(".before").click(function(){            
        $("#wrap").before("<p class='siblings'>我是同級元素before</p>");        });                
    //insertAfter(),將元素追加到指定對象的后面(是同級關系)        
    $(".insertAfter").click(function(){            
        $("<p class='three'>我是同級元素insertAfter</p>").insertAfter($("#wrap"));        });        
    //insertBefore(),將元素追加到指定對象的前面(是同級關系)        
    $(".insertBefore").click(function(){            
        $("<p class='three'>我是同級元素insertBefore</p>").insertBefore($("#wrap"));        });    
});            
    //appendChild(),在節點的最后追加子元素    
    function appChild(){
        // 創建p節點            
        var para=document.createElement("p");            
        // 創建文本節點            
        var node=document.createTextNode("我是子集appendChild新段落。");            
        // 把文本節點添加到p節點里            
        para.appendChild(node);                         
        // 查找div1            
        var element=document.getElementById("wrap");            
        // 把p節點添加到div1里            
        element.appendChild(para);    
    }
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM