我們 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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP