當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JQuery merge()用法及代碼示例


jQuery中的merge()方法用於將兩個數組的內容合並到第一個數組中。

用法:

jQuery.merge( first, second )

參數:merge()方法僅接受上麵提到並在下麵描述的一個參數:



  • first:此參數是要合並的第一個array-like對象,第二個元素已添加。
  • second:此參數是第二個array-like對象,該對象合並到第一個未更改的對象中。
  • 返回值:它返回合並的數組。

    範例1:在此示例中,merge()方法僅合並兩個數組。

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
    <title>JQuery | merge() method</title>  
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script> 
      
    </head> 
    <body style="text-align:center;">  
          
        <h1 style="color:green">  
            GeeksForGeeks  
        </h1>  
          
        <h3>JQuery | merge() method</h3> 
          
        <b>First = [ "a", "b", "c" ]</b><br> 
        <b>Second = [ "d", "e", "f" ]</b> 
        <br> 
        <p>Click on button to merge</p> 
        <button onclick="geek()">Click</button> 
        <br><br> 
        <b id="geeks"></b> 
          
        <script> 
        function geek() { 
            var first = [ "a", "b", "c" ]; 
            var second = [ "d", "e", "f" ]; 
            var n = $.merge(first, second ); 
            document.getElementById("geeks").innerHTML = "Merged:[" + n + "]"; 
        } 
        </script> 
    </body> 
    </html>                                                                                                    

    Output:
    Before Click:

    After Click:

    範例2:在此的示例merge()方法合並兩個以上的數組。

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="utf-8"> 
    <title>JQuery | merge() method</title>  
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script> 
      
    </head> 
    <body style="text-align:center;">  
          
        <h1 style="color:green">  
            GeeksForGeeks  
        </h1>  
          
        <h3>JQuery | merge() method</h3> 
          
        <b>Array_1 = [ "a", "b", "c" ]</b><br> 
        <b>Array_2 = [ "d", "e", "f" ]</b>    <br> 
        <b>Array_3 = [ 1, 2, 4, 5 ]</b>    <br> 
        <b>Array_4 = [ 3, 6, 9 ]</b><br> 
        <b>Array_5 = [ ]</b> 
        <br> 
        <p>Click on button to merge</p> 
        <button onclick="geek()">Click</button> 
        <br><br> 
        <b id="geeks"></b> 
          
        <script> 
        function geek() { 
            var fi = [ "a", "b", "c" ]; 
            var se = [ "d", "e", "f" ]; 
            var th = [ 1, 2, 4, 5 ]; 
            var fo = [ 3, 6, 9 ]; 
            var fif = []; 
            var n = $.merge( 
    th, $.merge($.merge(fif, $.merge(fi, se )), fo)); 
      
            document.getElementById( 
    "geeks").innerHTML = "Merged:[" + n + "]"; 
        } 
        </script> 
    </body> 
    </html>                                                

    Output:
    Before Click:

    After Click:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 JQuery | merge() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。