我們在 jQuery 中使用 $.extend() 方法嗎?


jQuery.extend() 方法用於將兩個或更多物件的內容合併在一起。物件將合併到第一個物件中。

你可以嘗試執行以下程式碼來學習如何使用 extend() 方法 -

示例

線上演示

<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $("#button1").click(function() {
    var obj1 = {
      maths: 60,
      history: {pages: 150,price: 400,lessons: 30},
      science: 120
    };
    var obj2 = {
      history: { price: 150, lessons: 24 },
      economics: 250
    };
    $.extend(true, obj1, obj2);
    $("#demo").append(JSON.stringify(obj1));
   });
});
</script>
</head>
<body>
   <button id="button1">Result</button>
   <div id="demo"></div>
</body>
</html>

更新時間:15-6-2020

1K+ 瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.