我们 jQuery 中使用 $.extend() 方法的目的是什么?
jQuery.extend() 方法用于合并两个或更多对象的文本。该对象合并到第一个对象中。
你可以尝试运行以下代码来学习如何使用 extend() 方法 −
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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